2015-08-19 89 views
0

內部服務器錯誤是一般錯誤,但在我的情況下是使用JPA與REST綁定。要獲得此錯誤,我需要返回豆列表:Restful Web Service javax.ws.rs.InternalServerErrorException:HTTP 500內部服務器錯誤

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) { 
    } 

} 

如果我想返回字符串或簡單的東西,一切都很好。如果我想要返回更復雜的東西,WS就會失敗。

回答

0

問題出在bean上。如果我能夠正確讀取堆棧跟蹤,我將節省大量的時間:

2015-08-19T10:48:02.329+0200|Info: CDI support is enabled 

2015-08-19T10:48:02.339 + 0200 |信息:啓動應用新澤西州,版本「澤西:1.19 02/11/2015 05:39 AM' 2015-08-19T10:48:02.820 + 0200 | Info:綁定EJB類resources.SimpleRESTResource到EJBManagedComponentProvider 2015-08-19T10:48:02.856 + 0200 | Info:加載應用程序[HelloGlassfish ] at [/ HelloGlassfish] 2015-08-19T10:48:02.873 + 0200 |信息:HelloGlassfish已成功部署在3,534毫秒。 2015-08-19T10:52:45.657 + 0200 |信息:啓動Jersey應用程序,版本澤西島:2.10.4 2014-08-08 15:09:00 ... 2015-08-19T10:52:46.118 + 0200 |信息:Hello1 Petr Hariprasad! 2015-08-19T10:52:46.120 + 0200 |信息:Hello2 Petr Hariprasad! 2015-08-19T10:52:46.175 + 0200 |嚴重:javax.ws.rs.InternalServerErrorException:HTTP 500內部服務器錯誤 at org.glassfish.jersey.message.internal.AbstractCollectionJaxbProvider.writeTo(AbstractCollectionJaxbProvider.java:282) 在org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:263) ... 造成的:com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:IllegalAnnotationExceptions 1個計數 model.CustomerRestSchema 沒有無參數默認構造函數。 這個問題涉及到以下位置: 在model.CustomerRestSchema 在com.sun.xml.bind.v2.runtime.IllegalAnnotationsException $ Builder.check(IllegalAnnotationsException.java:106)

有一個bean:

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; 
    } 

} 

我相信,它會對某人有用。