我在访问jax-rs资源时收到了以下堆栈跟踪。
我正在使用Tomcat 7,Jersey 1.12和Hibernate 4以及MySQL。
我在搜索解决方案时发现了这个教程:http://aruld.info/handling-generified-collections-in-jersey-jax-rs/但是列出的示例似乎都没有用。
我在这里缺少什么?
请不要让我写MessageBodyWriters
的答案,这应该可以解决问题。 (而且我知道有一个解决方案,我无法弄明白。)
这是我的所有罐子:
antlr-2.7.7.jar
asm-3.1.jar
commons-collections-3.2.1.jar
dom4j-1.6.1.jar
gson-1.7.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jackson-core-asl-1.9.2.jar
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-xc-1.9.2.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.CR2.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
jersey-client-1.12.jar
jersey-core-1.12.jar
jersey-json-1.12.jar
jersey-server-1.12.jar
jersey-servlet-1.12.jar
jettison-1.1.jar
jsr311-api-1.1.1.jar
mysql-connector-java-3.1.12-bin.jar
这是我的资源类和方法:
@Path("/region")
public class RegionService {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.APPLICATION_JSON)
public JResponse<List<Region>> region() {
RegionDao regionDao = new RegionDao();
regionDao.openSession();
List<Region> regions = regionDao.getAll();
regionDao.closeSession();
return JResponse.ok(regions).build();
}
}
这是堆栈跟踪:
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<campher.hibernate.entities.Region>, and MIME media type application/json was not found
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1451)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
答案 0 :(得分:36)
您需要使用@XmlRootElement注释Region类。
答案 1 :(得分:9)
作为一种快速解决方案,我看到了:
final List<MyModel> myList = new ArrayList<>();
Response.ok().entity(new GenericEntity<List<MyModel>>(myList) {}).build();
答案 2 :(得分:8)
另一个答案对我不起作用(我已经有了XmlRootElement注释),但我终于让它与JSON一起工作了。它正好返回XML,而不是JSON。
我正在使用jersey-bundle-1.17.jar(也尝试将asm-3.1.jar和jersey-json-1.17.jar添加到classpath中但仍然无效)。我终于尝试下载包含12个不同罐子的拉链。一旦我将所有12个罐子添加到我的类路径中,我终于摆脱了错误,并且很好地返回了JSON。
我希望这有助于某人。
编辑:这是包含12个jar文件的zip文件的链接:jersey-archive-1.17.zip
答案 3 :(得分:2)
带有此注释的@XmlRootElement可以在Jersey中选择JSON ou XML。 非常感谢你!
加上休息吧: @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
如果您在xml或json中获得了回报,请在标题上设置请求。
答案 4 :(得分:1)
正如您所期望的List,您可以使用@XmlRootElement注释您的bean类。但同样不适用于Map。 泽西岛内部使用moxy来完成工作。
答案 5 :(得分:0)
添加依赖
srand(static_cast<unsigned int>(time(0)));
答案 6 :(得分:-2)
You can use JSONObject to create a Json Response
eg:
List<Region> regions = regionDao.getAll();
JSONArray list = new JSONArray();
for(Region region : regions )
{
JSONObject jObject= new JSONObject();
//put all the data in json object
jObject.put(region.getSomething());
// and put this Jsonobject in JsonArray
list.add(jObject);
}
Or
Response.ok().entity(new GenericEntity<List<Region>>(regions) {}).build();
PS:如果它不适合你,请不要对答案进行投票。
答案 7 :(得分:-2)
在您的班级区域
之前添加@@XmlRootElement