ResponseBuilder - Long无法强制转换为Collection?

时间:2017-12-07 18:43:25

标签: java rest classcastexception

我有这个: Map<String, Object> social = new HashMap<String, Object>(); social.put("socialId", socialId); social.put("savedComment", savedComment);

我尝试这样做:builder = Response.status(Response.Status.OK).entity(social);

为什么我收到HTTP状态500 - java.lang.ClassCastException:java.lang.Long无法强制转换为java.util.Collection,因为Object应该使用Long和一般Object类?我不能发送两种不同类型的数据,因此数据包含data.socialId(作为Long)和data.savedComment(作为对象)?

*顺便说一句,socialId是一个Long对象,savedComment是一个SocialComment对象。

1 个答案:

答案 0 :(得分:1)

如果您查看entity()的API文档,则说明

  

响应实体的任何Java类型实例,受其支持   运行时可以传递。打包者有责任   具有GenericEntity的实际实体,如果保留其泛型   类型是必需的。

因此,如果您需要在响应中保留其类型,那么您的地图需要支持GenericEntity

来源:https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/Response.ResponseBuilder.html#entity-java.lang.Object-