Probleme mit Restful und JPA (Jersery, JPA, EclipseLink, Dali)


Andyx1975

Mitglied
Hallo,

ich habe ein Problem mit Restful...ich will eine SmartGWT app mit einem Webservice verbinden. Hierzu benutze ich Jersey und JPA mit Dali und EclipseLink. Ich habe mich an ein Tutorial von Eclipse Link gehalten. Der Webservice wird gestartet. Ich kann ich Ihn jedoch nicht mit "http://localhost:9997/CustomerService/rest/customers/1" aufrufen. Ich bekomme einen Download im Anschluss angezeigt von einer Datei die "1" heißt. Als Testcontainer benutze ich Jetty in der GWT/SmartGWT standard einstellung. Wie man auf dem Screenshot sieht scheint dies auch zu funktionieren. Der Webservice wird wohl gestartet.

Der JPA Part scheint zu funktionieren. Ich kann jedenfalls die Tables in der Datenbank über Eclipse erstellen lassen. Kann mir jemand weiterhelfen? Was mache ich falsch? Die Fehlermeldung ist im Anhang.

Danke Andy

Meine web.xml sieht wie folgt aus:
Java:
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <persistence-context-ref>
        <persistence-context-ref-name>persistence/em</persistence-context-ref-name>
        <persistence-unit-name>CustomerService</persistence-unit-name>
    </persistence-context-ref>
</web-app>

CustomerService-Klasse:
Java:
package uds.web.admin.server;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

 
@Stateless
@LocalBean
@Path("/customers")

public class CustomerService {
 
    @PersistenceContext(unitName="CustomerService", 
                        type=PersistenceContextType.TRANSACTION)
    
    EntityManager entityManager;

    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("{id}")
    public Customer read(@PathParam("id") long id) {
        return entityManager.find(Customer.class, id);
    }
 
    @POST
    @Consumes(MediaType.APPLICATION_XML)
    public void create(Customer customer) {
        entityManager.persist(customer);
    }
    
    @PUT
    @Consumes(MediaType.APPLICATION_XML)
    public void update(Customer customer) {
        entityManager.merge(customer);
    }
 
    @DELETE
    @Path("{id}")
    public void delete(@PathParam("id") long id) {
        Customer customer = read(id);
        if(null != customer) {
            entityManager.remove(customer);
        }
    }
 
    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("findCustomersByCity/{city}")
    public List<Customer> findCustomersByCity(@PathParam("city") String city) {
        Query query = entityManager.createNamedQuery("findCustomersByCity");
        query.setParameter("city", city);
        return query.getResultList();
    }
 
}

Customer Klasse:
Java:
package uds.web.admin.server;

import java.io.Serializable;
import javax.persistence.*;

/**
 * Entity implementation class for Entity: Customer
 *
 */
import java.io.Serializable;
import javax.persistence.*;
import java.util.Set;
 
import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
 
import java.util.Set;
 
@Entity
@XmlRootElement
@NamedQuery(name = "findCustomerByCity", 
query = "SELECT c " +
        "FROM Customer c " +
        "WHERE c.address.city = :city")
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
 
    @Id
    private long id;
 
    @Column(name="FIRST_NAME")
    private String firstName;
 
    @Column(name="LAST_NAME")
    private String lastName;
 
    @OneToOne(mappedBy="customer", cascade={CascadeType.ALL})
    private Address address;
 
    @OneToMany(mappedBy="customer", cascade={CascadeType.ALL})
    private Set<PhoneNumber> phoneNumbers;
 
    public long getId() {
        return this.id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getFirstName() {
        return this.firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
    public String getLastName() {
        return this.lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
    public Address getAddress() {
        return this.address;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
    public Set<PhoneNumber> getPhoneNumbers() {
        return this.phoneNumbers;
    }
 
    public void setPhoneNumbers(Set<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }
 
}

Address Klasse:
Java:
package uds.web.admin.server;

import java.io.Serializable;
import javax.persistence.*;

/**
 * Entity implementation class for Entity: Adress
 *
 */
import java.io.Serializable;
import javax.persistence.*;
 
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
 
@Entity
public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
 
    @Id
    private long id;
 
    private String city;
 
    private String street;
 
    @OneToOne
    @PrimaryKeyJoinColumn
    private Customer customer;
 
    public long getId() {
        return this.id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getCity() {
        return this.city;
    }
 
    public void setCity(String city) {
        this.city = city;
    }
 
    public String getStreet() {
        return this.street;
    }
 
    public void setStreet(String street) {
        this.street = street;
    }
 
    @XmlInverseReference(mappedBy="address")
    public Customer getCustomer() {
        return customer;
    }
 
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
 
}

PhoneNumber Klasse:
Java:
package uds.web.admin.server;

import java.io.Serializable;
import javax.persistence.*;

/**
 * Entity implementation class for Entity: PhoneNumber
 *
 */
import java.io.Serializable;
import javax.persistence.*;
 
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
 
@Entity
@Table(name="PHONE_NUMBER")
public class PhoneNumber implements Serializable {
    private static final long serialVersionUID = 1L;
 
    @Id 
    private long id;
 
    private String num;
 
    private String type;
 
    @ManyToOne
    @JoinColumn(name="ID_CUSTOMER")
    private Customer customer;
 
    public long getId() {
        return this.id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getNum() {
        return this.num;
    }
 
    public void setNum(String num) {
        this.num = num;
    }
 
    public String getType() {
        return this.type;
    }
 
    public void setType(String type) {
        this.type = type;
    }
 
    @XmlInverseReference(mappedBy="phoneNumbers")
    public Customer getCustomer() {
        return this.customer;
    }
 
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
 
}
 

Anhänge

  • Error.jpg
    Error.jpg
    502,7 KB · Aufrufe: 14

Andyx1975

Mitglied
Kann es sein dass ich den falschen Link zum Aufruf des Webservices verwende? Der Aufruf GWT.getModuleBaseURL() ergibt folgendes Erbenis: http://127.0.0.1:8888/uds.web.admin.webAdminUds/


Meine web.xml sieht wie folgt aus:
Java:
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <persistence-context-ref>
        <persistence-context-ref-name>persistence/em</persistence-context-ref-name>
        <persistence-unit-name>CustomerService</persistence-unit-name>
    </persistence-context-ref>
</web-app>

Consolenausgabe:
Java:
INFO: Scanning for root resource and provider classes in the Web app resource paths:
  /WEB-INF/lib
  /WEB-INF/classes
08.08.2011 15:07:01 com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class uds.web.admin.data.CustomerService
08.08.2011 15:07:01 com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Provider classes found:
  class org.codehaus.jackson.jaxrs.JsonParseExceptionMapper
  class org.codehaus.jackson.jaxrs.JsonMappingExceptionMapper
  class org.codehaus.jackson.jaxrs.JacksonJsonProvider
  class org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider
08.08.2011 15:07:01 com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.8 06/24/2011 12:17 PM'
 

Neue Beiträge

Forum-Statistiken

Themen
272.360
Beiträge
1.558.632
Mitglieder
187.834
Neuestes Mitglied
jordanx0206