简体   繁体   English

Json在Jersey REST应用程序中响应严重错误

[英]Json response severe error in jersey REST application

I'm learning JERSEY REST API with maven. 我正在使用Maven学习JERSEY REST API。 I'm getting following error whey i hit GET request. 我遇到以下错误,我遇到了GET请求。 Interesting point is when i return only specific class object, i'm getting the desired result but when i return a map of all those objects i'm getting this error with Status 500 - Internal Server Error. 有趣的一点是,当我仅返回特定的类对象时,就得到了所需的结果,但是当我返回所有这些对象的映射时,就遇到了状态500-内部服务器错误的错误。 Please suggest. 请提出建议。

Error: 错误:

SEVERE: MessageBodyWriter not found for media type=application/json, 
type=class java.util.HashMap, genericType=java.util.Map<java.lang.Integer, message.Message>.

Error prone: 容易出错:

@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<Integer,Message> getAll(){
    ms.sample();
    return ms.getAllMessages();

}

Same code but returning specific object, works fine: 相同的代码,但返回特定的对象,工作正常:

@GET
@Produces(MediaType.APPLICATION_JSON)
public Message getAll(){
    ms.sample();
    return ms.getAllMessages().get(1);

}

PS : I've added json related element in pom.xml file as follows PS:我在pom.xml文件中添加了与json相关的元素,如下所示

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

I'm using Maven v2.16 我正在使用Maven v2.16

Add following dependency 添加以下依赖

<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-json-jackson</artifactId>

</dependency>

MOXy and Map s are not friends... I recommend you using Jackson instead of MOXy to (de)serialize JSON. MOXy和Map不是朋友……我建议您使用Jackson而不是MOXy对JSON进行反序列化。

To do it, remove the jersey-media-moxy dependency and add the following: 为此,请删除jersey-media-moxy依赖性并添加以下内容:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.23.1</version>
</dependency>

For more details, check Jersey documentation . 有关更多详细信息,请参阅Jersey文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM