Restful Web Service javax.ws.rs.InternalServerErrorException:HTTP 500内部服务器错误

时间:2015-08-19 09:21:19

标签: java web-services rest jpa

内部服务器错误是一般错误,但在我的情况下是使用JPA与REST绑定。要获得此错误,我需要返回Bean列表:

package resources;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import model.CustomerRest;
import model.CustomerRestSchema;


//@ManagedBean
@Stateless
@Path("simplerest")
public class SimpleRESTResource {

    private final static Logger logger = Logger.getLogger(SimpleRESTResource.class.getName());

    //HelpExceptionMapper helpEx = new HelpExceptionMapper();

    @Context
    private UriInfo context;

    @PersistenceUnit(unitName = "jdbc/Test")
    EntityManagerFactory emf;

    /**
     * Default constructor. 
     */
    public SimpleRESTResource() {
        // TODO Auto-generated constructor stub
    }

    private List<CustomerRestSchema> convertCustomers(List<CustomerRest> customers) {
      List<CustomerRestSchema> customersSimplified = new ArrayList<>();

      for(CustomerRest cust : customers) {
        customersSimplified.add(new CustomerRestSchema(cust.getId(), cust.getFirstName(), cust.getLastName()));
      }
      return customersSimplified;
    }

    private List<CustomerRestSchema> getCustomers() {
        List<CustomerRest> customers = null;
        List<CustomerRestSchema> customersrest = null;
        String result = null;
        try {
            EntityManager em = emf.createEntityManager();
            //CustomerRest cure =  (CustomerRest) emf.createEntityManager().createQuery("select u from CustomerRest u", CustomerRest.class).getResultList().get(0);
            customers =  (List<CustomerRest>) em.createNamedQuery("CustomerRest.findAll", CustomerRest.class).getResultList();
            CustomerRest cust = customers.get(0);
            result = "<greeting>Hello1 " + cust.getFirstName() + " " +cust.getLastName() + /*" " + cust.getAddressRests().get(0).getStreet() +*/ "!</greeting>";
            logger.info(result);

            customersrest = (new Customers(convertCustomers(customers))).getCustomers();
            CustomerRestSchema custrest = customersrest.get(0);

            result = "<greeting>Hello2 " + custrest.getFirstName() + " " +custrest.getLastName() + /*" " + cust.getAddressRests().get(0).getStreet() +*/ "!</greeting>";
            logger.info(result);
        }
        catch (Exception ee) {
          ee.printStackTrace();
          //throw new UnsupportedOperationException();
        }
        return customersrest;       
    }

    /**
     * Retrieves representation of an instance of SimpleResource
     * @return an instance of String
     */
    @GET
    @Path("custxml")
    @Produces(MediaType.APPLICATION_XML)
    public List<CustomerRestSchema> getXml() {
        return getCustomers();
    }

    /**
     * Retrieves representation of an instance of SimpleResource
     * @return an instance of String
     */
    @GET
    @Path("custjson")
    @Produces(MediaType.APPLICATION_JSON)
    public List<CustomerRestSchema> getJson() {
        return getCustomers();
    }

    /**
     * PUT method for updating or creating an instance of SimpleResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes(MediaType.APPLICATION_XML)
    public void putXml(String content) {
    }

}

如果我想要返回String或简单的东西,一切都很好。如果我想返回更复杂的东西,WS就会失败。

1 个答案:

答案 0 :(得分:0)

问题在于bean。如果我能够正确读取堆栈跟踪,我将节省大量时间:

2015-08-19T10:48:02.329+0200|Info: CDI support is enabled
2015-08-19T10:48:02.339+0200|Info: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 05:39 AM'
2015-08-19T10:48:02.820+0200|Info: Binding the EJB class resources.SimpleRESTResource to EJBManagedComponentProvider
2015-08-19T10:48:02.856+0200|Info: Loading application [HelloGlassfish] at [/HelloGlassfish]
2015-08-19T10:48:02.873+0200|Info: HelloGlassfish was successfully deployed in 3,534 milliseconds.
2015-08-19T10:52:45.657+0200|Info: Initiating Jersey application, version Jersey: 2.10.4 2014-08-08 15:09:00...
2015-08-19T10:52:46.118+0200|Info: <greeting>Hello1 Petr Hariprasad!</greeting>
2015-08-19T10:52:46.120+0200|Info: <greeting>Hello2 Petr Hariprasad!</greeting>
2015-08-19T10:52:46.175+0200|Severe: javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
    at org.glassfish.jersey.message.internal.AbstractCollectionJaxbProvider.writeTo(AbstractCollectionJaxbProvider.java:282)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:263)
...
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
model.CustomerRestSchema **does not have a no-arg default constructor**.
    this problem is related to the following location:
        at model.CustomerRestSchema
    at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)

有一个豆子:

package model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "customer")
public class CustomerRestSchema implements Serializable {
    /***/
    private static final long serialVersionUID = 1L;
    private long id;
    private String firstName;
    private String lastName;

    /** empty public constructor is important! */
    public CustomerRestSchema() {

    }

    /** constructor */
    public CustomerRestSchema(long id, String firstName, String lastName) {
      this.id = id;
      this.firstName = firstName;
      this.lastName = lastName;
    }


    public long getId() {
        return id;
    }

    @XmlElement(name = "id")
    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    @XmlElement
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @XmlElement(name = "lastName")
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

我相信,这对某些人有用。