I received the below stack trace when accessing on of my jax-rs resources.
在访问我的jax-rs资源时,我收到了下面的堆栈跟踪。
I'm using Tomcat 7, with Jersey 1.12 and Hibernate 4 and MySQL.
我使用的是Tomcat 7,还有Jersey 1.12和Hibernate 4和MySQL。
I found this tutorial while searching for a solution: http://aruld.info/handling-generified-collections-in-jersey-jax-rs/ but none of the examples listed seemed to work.
在寻找解决方案时,我找到了这个教程:http://aruld.info/handling- generifiedcollections -in-jersey-jax-rs/,但是没有列出的示例似乎是有效的。
What am I missing here?
我错过了什么?
Please no answers that have me writing MessageBodyWriters
, this should work out the box. (And I know there's a solution, I just can't figure it out.)
请不要回答让我写留言的人,这应该能解决问题。(我知道有一个解决办法,我就是想不出来。)
Here are all my jars:
这是我所有的罐子:
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
Here is my resource class and method:
这里是我的资源类和方法:
@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();
}
}
And here is the stacktrace:
这里是stacktrace:
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)
8 个解决方案
#1
36
You need to annotate Region class with @XmlRootElement.
您需要使用@XmlRootElement来注释区域类。
#2
9
as a quick solution I see:
作为一个快速解决方案,我看到:
final List<MyModel> myList = new ArrayList<>();
Response.ok().entity(new GenericEntity<List<MyModel>>(myList) {}).build();
#3
8
The other answer didn't work for me (I already had the XmlRootElement annotation), but I finally got it to work with JSON. It was returning XML just fine, just not JSON.
另一个答案对我不起作用(我已经有了XmlRootElement注释),但是我终于让它与JSON一起工作了。它返回的是XML,不是JSON。
I was using the jersey-bundle-1.17.jar (also tried with the asm-3.1.jar and jersey-json-1.17.jar added to classpath and still didn't work). I finally just tried downloading the zip that includes 12 different jars. Once I added all 12 jars to my classpath I finally got rid of the error and works great returning JSON.
我用的是jersey- bund1 -1.17。jar(也尝试使用asm-3.1。jar和jersey - json - 1.17。jar添加到类路径,仍然没有工作。我最后尝试下载了包含12个不同jar的zip文件。在我的类路径中添加了所有的12个jar之后,我终于摆脱了这个错误,并且返回了JSON。
I hope this helps somebody.
我希望这能帮助别人。
EDIT: Here is a link to the zip file that contains the 12 jar files: jersey-archive-1.17.zip
编辑:这里是一个包含12个jar文件的zip文件的链接:jersey-archive-1.17.zip。
#4
2
@XmlRootElement with this annotation it's possible to select JSON ou XML in Jersey. Thank you very much!
使用此注释的@XmlRootElement可以在Jersey中选择JSON ou XML。非常感谢!
Just add to rest this: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
只需添加rest: @Produces({MediaType)。APPLICATION_JSON MediaType.APPLICATION_XML })
On the request you set on the header if you won the return in xml or in json!
如果您在xml或json中获得了返回值,请在标题上设置该请求。
#5
1
As you are expecting List, you can annotate your bean class with @XmlRootElement. But the same won't work for Map. Jersey internally uses moxy to do the job done.
正如您所期待的列表,您可以使用@XmlRootElement来注释您的bean类。但是同样的方法在地图上行不通。Jersey内部使用moxy来完成任务。
#6
0
Add dependency
添加依赖关系
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey-version}</version>
</dependency>
#7
-1
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();
#8
-2
add @@XmlRootElement
before your class Region
在类区域之前添加@@XmlRootElement。
#1
36
You need to annotate Region class with @XmlRootElement.
您需要使用@XmlRootElement来注释区域类。
#2
9
as a quick solution I see:
作为一个快速解决方案,我看到:
final List<MyModel> myList = new ArrayList<>();
Response.ok().entity(new GenericEntity<List<MyModel>>(myList) {}).build();
#3
8
The other answer didn't work for me (I already had the XmlRootElement annotation), but I finally got it to work with JSON. It was returning XML just fine, just not JSON.
另一个答案对我不起作用(我已经有了XmlRootElement注释),但是我终于让它与JSON一起工作了。它返回的是XML,不是JSON。
I was using the jersey-bundle-1.17.jar (also tried with the asm-3.1.jar and jersey-json-1.17.jar added to classpath and still didn't work). I finally just tried downloading the zip that includes 12 different jars. Once I added all 12 jars to my classpath I finally got rid of the error and works great returning JSON.
我用的是jersey- bund1 -1.17。jar(也尝试使用asm-3.1。jar和jersey - json - 1.17。jar添加到类路径,仍然没有工作。我最后尝试下载了包含12个不同jar的zip文件。在我的类路径中添加了所有的12个jar之后,我终于摆脱了这个错误,并且返回了JSON。
I hope this helps somebody.
我希望这能帮助别人。
EDIT: Here is a link to the zip file that contains the 12 jar files: jersey-archive-1.17.zip
编辑:这里是一个包含12个jar文件的zip文件的链接:jersey-archive-1.17.zip。
#4
2
@XmlRootElement with this annotation it's possible to select JSON ou XML in Jersey. Thank you very much!
使用此注释的@XmlRootElement可以在Jersey中选择JSON ou XML。非常感谢!
Just add to rest this: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
只需添加rest: @Produces({MediaType)。APPLICATION_JSON MediaType.APPLICATION_XML })
On the request you set on the header if you won the return in xml or in json!
如果您在xml或json中获得了返回值,请在标题上设置该请求。
#5
1
As you are expecting List, you can annotate your bean class with @XmlRootElement. But the same won't work for Map. Jersey internally uses moxy to do the job done.
正如您所期待的列表,您可以使用@XmlRootElement来注释您的bean类。但是同样的方法在地图上行不通。Jersey内部使用moxy来完成任务。
#6
0
Add dependency
添加依赖关系
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey-version}</version>
</dependency>
#7
-1
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();
#8
-2
add @@XmlRootElement
before your class Region
在类区域之前添加@@XmlRootElement。