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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19089781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:59:29  来源:igfitidea点击:

RestEasy: Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/json

javajsonresteasy

提问by Balaji Krishnan

message: Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: 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

描述:服务器遇到内部错误(找不到 MessageBodyWriter 类型的响应对象:java.util.ArrayList of media type: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 Hymanson provider in the classpath. Tried changing the return type form ArrayListto List, then wrapping it in GenericEntitybased on resteasy response, but still getting the same issue.

我在类路径中有 resteasy Hymanson 提供程序。尝试将返回类型表单更改ArrayListList,然后GenericEntity根据resteasy response将其包装,但仍然遇到相同的问题。

Running on tomcat7.

在 tomcat7 上运行。

Thanks.

谢谢。

采纳答案by Balaji Krishnan

finally solved it using the Gson libraryinstead of relying on json. did not wrap in Generic Entity either. Here is the code that works

最终使用Gson library而不是依赖json解决了它。也没有包含在通用实体中。这是有效的代码

@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错误(休眠双向映射)

回答by Hemanth

I solved this exception by adding resteasy-Hymanson-provider.jar to classpath Refer https://bitbucket.org/arcbees/gaestudio/issue/2/need-resteasy-Hymanson-provider-on

我通过将 resteasy-Hymanson-provider.jar 添加到 classpath 解决了这个异常 参考https://bitbucket.org/arcbees/gaestudio/issue/2/need-resteasy-Hymanson-provider-on

回答by Surendran Duraisamy

Faced same issue resolved by adding @XMLRootElement in class used in ArrayList

面临通过在 ArrayList 中使用的类中添加 @XMLRootElement 解决的相同问题

回答by Miraj Hamid

By adding this dependency I was able to solve this issue.

通过添加此依赖项,我能够解决此问题。

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-Hymanson</artifactId>
    <version>2.10.1</version>
</dependency>