简体   繁体   English

RestEasy:找不到类型的响应对象的MessageBodyWriter:java.util.Array媒体类型列表:application / json

[英]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.

相关问题 找不到响应 object 类型的 MessageBodyWriter:java.util.ArrayList 媒体类型:text/html - 在 Resteasy 中 - Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: text/html - in Resteasy 找不到 MessageBodyWriter 类型的响应对象:java.util.LinkedHashMap 媒体类型:application/json - Could not find MessageBodyWriter for response object of type: java.util.LinkedHashMap of media type: application/json 找不到媒体类型=应用程序/json,类型=类 java.util.ArrayList 的 MessageBodyWriter - MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure:无法找到类型的响应对象的MessageBodyWriter:媒体类型:application / xml - org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: of media type: application/xml 未找到 Media type=text/plain、type=class java.util.ArrayList、genericType=java.util.List 的 MessageBodyWriter<models.Person> - MessageBodyWriter not found for media type=text/plain, type=class java.util.ArrayList, genericType=java.util.List<models.Person> 严重:未找到 Java 类 java.util.ArrayList 和 MIME 媒体类型 application/json 的消息正文编写器 - SEVERE: A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found 找不到Java类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器 - A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found 找不到Java类java.util.ArrayList和Java类型类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器 - A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/json was not found 无法为类型的响应对象找到MessageBodyWriter:com.sun.jersey.api.json.JSONWithPadding媒体类型:application / x-javascript - Could not find MessageBodyWriter for response object of type: com.sun.jersey.api.json.JSONWithPadding of media type: application/x-javascript 找不到Java类型类java.util.ArrayList和MIME媒体类型application / xml的消息正文编写器 - A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM