內部服務器錯誤是一般錯誤,但在我的情況下是使用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就會失敗。