RestEasy:找不到类型为java.util.Array的响应对象的MessageBodyWriter媒体类型为:application/json [英] RestEasy: Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/json

查看:112
本文介绍了RestEasy:找不到类型为java.util.Array的响应对象的MessageBodyWriter媒体类型为:application/json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

说明:服务器遇到内部错误(找不到 类型为:java.util.ArrayList的响应对象的MessageBodyWriter 媒体类型: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

@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();
}

我在类路径中有resteasy jackson提供程序. 尝试将返回类型从ArrayList更改为List,然后根据 resteasy响应GenericEntity中>,但仍然遇到相同的问题.

I have got the resteasy jackson provider in the classpath. Tried changing the return type form ArrayList to List, then wrapping it in GenericEntity based on resteasy response, but still getting the same issue.

在tomcat7上运行.

Running on tomcat7.

谢谢.

推荐答案

最终使用Gson library而不是依靠json来解决它. 也没有包装在通用实体中.这是有效的代码

finally solved it using the Gson library instead of relying on 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();
}

必须使用排除策略来避免循环引用.这是该链接: json转换过程中的stackoverflow错误(休眠双向映射)

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)

这篇关于RestEasy:找不到类型为java.util.Array的响应对象的MessageBodyWriter媒体类型为:application/json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆