[英]RestEasy: Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/json
message: Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/json
消息:找不到类型的响应对象的MessageBodyWriter:java.util.Array媒体类型列表:application / json
Description: The server encountered an internal error (Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/json) that prevented it from fulfilling this request
说明:服务器遇到内部错误(找不到类型的响应对象的MessageBodyWriter:媒体类型的java.util.ArrayList:application / json)阻止它完成此请求
@GET
@Path("/{userName}/questions")
//@Produces("application/json")
public Response getUserQuestions(@PathParam("userName") String userName){
UserDAO userDAO = new UserDAO();
List<Question> questions = userDAO.getUserQuestionsByUserName(userName);
GenericEntity<List<Question>> entity = new GenericEntity<List<Question>>(questions){};
return Response.status(200).entity(entity).type(MediaType.APPLICATION_JSON).build();
}
I have got the resteasy jackson provider in the classpath. 我在classpath中有resteasy jackson提供程序。 Tried changing the return type form
ArrayList
to List
, then wrapping it in GenericEntity
based on resteasy response , but still getting the same issue. 尝试将返回类型从
ArrayList
更改为List
,然后根据resteasy响应将其包装在GenericEntity
,但仍然遇到相同的问题。
Running on tomcat7. 在tomcat7上运行。
Thanks. 谢谢。
我通过在类路径中添加resteasy-jackson-provider.jar解决了这个异常。请参阅https://bitbucket.org/arcbees/gaestudio/issue/2/need-resteasy-jackson-provider-on
finally solved it using the Gson library
instead of relying on json. 最后使用
Gson library
解决了它,而不是依赖于json。 did not wrap in Generic Entity either. 也没有包装在通用实体中。 Here is the code that works
这是有效的代码
@GET
@Path("/{userName}/questions")
public Response getUserQuestions(@PathParam("userName") String userName){
UserDAO userDAO = new UserDAO();
List<Question> questions = userDAO.getQuestionsByUserName(userName);
Gson gson = new GsonBuilder().setExclusionStrategies(new UserQuestionsExclStrat()).create(); //.serializeNulls()
String json = gson.toJson(questions);
System.out.println(json);
return Response.status(200).entity(json).build();
}
Had to use the exclusion strategy to avoid cyclic reference. 不得不使用排除策略来避免循环引用。 here is the link for that: stackoverflow error during json conversion (hibernate bi-directional mapping)
这里是链接: json转换期间的stackoverflow错误(休眠双向映射)
通过在ArrayList中使用的类中添加@XMLRootElement来解决相同的问题
By adding this dependency I was able to solve this issue. 通过添加此依赖项,我能够解决此问题。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.10.1</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.