A message body writer for Java class not found

2019-01-08 14:26发布

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:

@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:

@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 ?

标签: java rest jax-rs
6条回答
甜甜的少女心
2楼-- · 2019-01-08 14:58

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

<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.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-08 14:58

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

...
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);
...
查看更多
Rolldiameter
4楼-- · 2019-01-08 15:05

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

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

<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>
查看更多
混吃等死
5楼-- · 2019-01-08 15:10

I added following jars and it worked for me

  • jackson-core-asl-1.9.2.jar
  • jackson-mapper-asl-1.9.2.jar
  • jackson-xc-1.9.2.jar
  • jackson-jaxrs-1.9.2.jar
查看更多
贼婆χ
6楼-- · 2019-01-08 15:22

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.

查看更多
Fickle 薄情
7楼-- · 2019-01-08 15:22

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

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.

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:

 ClientConfig clientConfig = new DefaultClientConfig();
 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,Boolean.TRUE);
 Client client = Client.create(clientConfig);
查看更多
登录 后发表回答