本文介绍了找不到Java类的消息正文编写器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

    }

}

我的数据只是一个POJO类:

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;
    }
}

我得到一个例外:

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

我做错了什么?

推荐答案

我终于找到了答案。我添加了

I finally found my answer. I added

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

到我的pom.xml文件。然后我添加了

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>

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

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

这篇关于找不到Java类的消息正文编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 03:21
查看更多