尝试返回XML响应时,Jersey返回500 [英] Jersey returns 500 when trying to return an XML response
问题描述
我正在尝试使用基于此文章.我想根据从URL传递的ID返回类的XML表示形式,但是,从 Advanced Rest Client Application (谷歌浏览器应用)尝试时,我得到了500个响应代码或浏览器.详细信息如下:
I'm trying to create my own RESTful WS application using Jersey 2.12 based from this article. I want to return an XML representation of a class depending on the id been passed from the url, however, I'm getting a 500 response code when trying from either Advanced Rest Client Application (google chrome app) or browser. Below are the details:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-
app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WS_RESTful_Practice</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>test.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
TestRestModel.java
TestRestModel.java
package test.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class TestRestModel{
/**
*
*/
private static final long serialVersionUID = -8391589100962515747L;
private String name;
private String content;
public TestRestModel(String name, String content){
this.name = name;
this.content = content;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
TestResource.java
TestResource.java
package test.services;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import test.dao.TestModelDao;
import test.model.TestRestModel;
@Path("/test")
public class TestResource {
@GET
@Path("{id}")
public Response getModel(@PathParam("id") String id){
return Response.ok().entity(TestModelDao.instance.getModel().get(id)).build();
}
}
TestModelDao.java
TestModelDao.java
package test.dao;
import java.util.HashMap;
import java.util.Map;
import test.model.TestRestModel;
public enum TestModelDao {
instance;
private Map<String, TestRestModel> container = new HashMap<String, TestRestModel>();
private TestModelDao(){
TestRestModel model = new TestRestModel("a", "this is first");
container.put("1", model);
model = new TestRestModel("b", "this is second");
container.put("2", model);
model = new TestRestModel("c", "this is third");
container.put("3", model);
}
public Map<String, TestRestModel> getModel(){
return container;
}
}
我对Jersey和REST完全陌生.而且我还不知道如何从Jersey记录错误.
I'm totally new to Jersey and REST. And I don't know how to log error from Jersey yet.
推荐答案
当您没有为JAXB bean提供默认的no arg构造函数时,就会发生这种情况.因此,在您的情况下,您应该通过添加以下内容来修改该类:
This happens when you don't provide a default no arg constructor to your JAXB bean. So in your case you should amend the class by adding one:
public TestRestModel(){
}
这是由于 JSR中的要求所致-222 :
由用户创建的现有类型必须提供一个无参数 构造函数.在编组期间,unmarshaller使用no arg构造函数 取消编组以创建该类型的实例.
existing types, authored by users, are required to provide a no arg constructor. The no arg constructor is used by an unmarshaller during unmarshalling to create an instance of the type.
这篇关于尝试返回XML响应时,Jersey返回500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!