找不到Java类的消息正文编写器

时间:2021-11-08 19:36:03

I am new to using JAX-RS and wrote a sample application that outputs a json object. but I am getting an exception. Here is my code:

我是新手使用JAX-RS并编写了一个输出json对象的示例应用程序。但我得到一个例外。这是我的代码:

@Path("/hello")
public class HelloWorldService {

    @GET
    @Path("/query/{artist_id}")
    @Produces("application/json")
    public Data getMsg(@PathParam("artist_id") int artist_id,
                            @QueryParam("from") int from,
                            @QueryParam("to") int to) {
        Data d=new Data();
        d.setName("Mateen");
        d.setRoll(77);
        return d;

    }

}

}

My data is simply a POJO class:

我的数据只是一个POJO类:

@XmlRootElement
public class Data {
    private int roll;
    private String name;
    public int getRoll() {
        return roll;
    }
    public void setRoll(int roll) {
        this.roll = roll;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

I get an exception:

我得到一个例外:

javax.ws.rs.WebApplicationException: 
    com.sun.jersey.api.MessageException: 
    A message body writer for Java class com.abc.data.Data, 
    and Java type class com.abc.data.Data, 
    and MIME media type application/json was not found

What am i doing wrong ?

我究竟做错了什么 ?

6 个解决方案

#1


94  

I finally found my answer. I added

我终于找到了答案。我补充道

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>

to my pom.xml file. Then I added

到我的pom.xml文件。然后我补充说

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

to my web.xml file, and everything works fine. No change was required to my code above.

到我的web.xml文件,一切正常。我的代码无需更改。

#2


16  

I added following jars and it worked for me

我添加了以下罐子,它对我有用

  • jackson-core-asl-1.9.2.jar
  • 杰克逊核心ASL-1.9.2.jar
  • jackson-mapper-asl-1.9.2.jar
  • 杰克逊映射器-ASL-1.9.2.jar
  • jackson-xc-1.9.2.jar
  • 杰克逊-XC-1.9.2.jar
  • jackson-jaxrs-1.9.2.jar
  • 杰克逊JAXRS-1.9.2.jar

#3


12  

Just a small addition. In case if you do not use the web.xml descriptor file, you may enable the POJOMappingFeatire programatically as shown below

只是一个小小的补充。如果您不使用web.xml描述符文件,您可以以编程方式启用POJOMappingFeatire,如下所示

...
final ResourceConfig rc = new PackagesResourceConfig("com.test.resources");
    final Map<String, Object> config = new HashMap<String, Object>();
    config.put("com.sun.jersey.api.json.POJOMappingFeature", true);
    rc.setPropertiesAndFeatures(config);
...

#4


5  

Maven User: You just need the following 2 dependencies.

<!-- For Jersey --> 
<dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.15</version>
</dependency>

<!-- For JSON --> 
<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.15</version>    
</dependency>

XML output is supported by default. So no dependency needed for it.

默认情况下支持XML输出。所以不需要任何依赖。

#5


2  

Working with Jersey 1.8, I was getting the same error while creating a JSON object and hitting a REST API from client side.

使用Jersey 1.8,我在创建JSON对象并从客户端访问REST API时遇到了同样的错误。

If you are facing issue at server side then, make sure that you have correctly included jersey-json.jar in classpath and mapped correct init-param in web.xml as mentioned in other answers.

如果您在服务器端遇到问题,请确保在类路径中正确包含了jersey-json.jar,并在其他答案中提到的web.xml中映射了正确的init-param。

For Client Side

As per Jersey 1.8 documentation, the following snippet shows how to use the POJO JSON mapping feature on the client side:

根据Jersey 1.8文档,以下代码段显示了如何在客户端使用POJO JSON映射功能:

 ClientConfig clientConfig = new DefaultClientConfig();
 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,Boolean.TRUE);
 Client client = Client.create(clientConfig);

#6


0  

I had the same issue: A message body writer for Java type, class java.lang.String, and MIME media type, application/json, was not found

我遇到了同样的问题:找不到Java类型的消息体编写器,类java.lang.String和MIME媒体类型application / json

The problem was that the class javax.ws.rs.ext.MessageBodyWriter was taken from

问题是类javax.ws.rs.ext.MessageBodyWriter取自

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

Was colliding with the same class from:

与同一类人发生冲突:

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19.1</version>
    </dependency>

#1


94  

I finally found my answer. I added

我终于找到了答案。我补充道

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>

to my pom.xml file. Then I added

到我的pom.xml文件。然后我补充说

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

to my web.xml file, and everything works fine. No change was required to my code above.

到我的web.xml文件,一切正常。我的代码无需更改。

#2


16  

I added following jars and it worked for me

我添加了以下罐子,它对我有用

  • jackson-core-asl-1.9.2.jar
  • 杰克逊核心ASL-1.9.2.jar
  • jackson-mapper-asl-1.9.2.jar
  • 杰克逊映射器-ASL-1.9.2.jar
  • jackson-xc-1.9.2.jar
  • 杰克逊-XC-1.9.2.jar
  • jackson-jaxrs-1.9.2.jar
  • 杰克逊JAXRS-1.9.2.jar

#3


12  

Just a small addition. In case if you do not use the web.xml descriptor file, you may enable the POJOMappingFeatire programatically as shown below

只是一个小小的补充。如果您不使用web.xml描述符文件,您可以以编程方式启用POJOMappingFeatire,如下所示

...
final ResourceConfig rc = new PackagesResourceConfig("com.test.resources");
    final Map<String, Object> config = new HashMap<String, Object>();
    config.put("com.sun.jersey.api.json.POJOMappingFeature", true);
    rc.setPropertiesAndFeatures(config);
...

#4


5  

Maven User: You just need the following 2 dependencies.

<!-- For Jersey --> 
<dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.15</version>
</dependency>

<!-- For JSON --> 
<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.15</version>    
</dependency>

XML output is supported by default. So no dependency needed for it.

默认情况下支持XML输出。所以不需要任何依赖。

#5


2  

Working with Jersey 1.8, I was getting the same error while creating a JSON object and hitting a REST API from client side.

使用Jersey 1.8,我在创建JSON对象并从客户端访问REST API时遇到了同样的错误。

If you are facing issue at server side then, make sure that you have correctly included jersey-json.jar in classpath and mapped correct init-param in web.xml as mentioned in other answers.

如果您在服务器端遇到问题,请确保在类路径中正确包含了jersey-json.jar,并在其他答案中提到的web.xml中映射了正确的init-param。

For Client Side

As per Jersey 1.8 documentation, the following snippet shows how to use the POJO JSON mapping feature on the client side:

根据Jersey 1.8文档,以下代码段显示了如何在客户端使用POJO JSON映射功能:

 ClientConfig clientConfig = new DefaultClientConfig();
 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,Boolean.TRUE);
 Client client = Client.create(clientConfig);

#6


0  

I had the same issue: A message body writer for Java type, class java.lang.String, and MIME media type, application/json, was not found

我遇到了同样的问题:找不到Java类型的消息体编写器,类java.lang.String和MIME媒体类型application / json

The problem was that the class javax.ws.rs.ext.MessageBodyWriter was taken from

问题是类javax.ws.rs.ext.MessageBodyWriter取自

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

Was colliding with the same class from:

与同一类人发生冲突:

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19.1</version>
    </dependency>