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

use*_*789 55 java rest jax-rs

我是新手使用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;

    }
Run Code Online (Sandbox Code Playgroud)

}

我的数据只是一个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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到一个例外:

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
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么 ?

use*_*789 98

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

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>
Run Code Online (Sandbox Code Playgroud)

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

  • 谢谢,它适合我.此外,在Jersey https://jersey.java.net/documentation/1.18/json.html的官方网页上也发现了相同的内容(至少第5.1 + 5.2节) (3认同)

Ser*_*oga 16

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

  • 杰克逊核心ASL-1.9.2.jar
  • 杰克逊映射器-ASL-1.9.2.jar
  • 杰克逊-XC-1.9.2.jar
  • 杰克逊JAXRS-1.9.2.jar


Cyr*_*eba 13

只是一个小小的补充.如果您不使用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);
...
Run Code Online (Sandbox Code Playgroud)

  • 请指出我到底要将上述代码行添加到何处? (2认同)

Sor*_*ter 5

Maven用户:您只需要以下2个依赖项.

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

默认情况下支持XML输出.因此不需要依赖.

  • 仅供参考,这仅适用于使用`web.xml`配置.如果你想使用`Applitation/ResourceConfig`子类进行配置,那么你需要`jersey-container-servlet`而不是`core`.前者具有`ServletContainerInitializer`,它初始化Jersey servlet,而不需要web.xml.如果你在2.5 servlet容器中,那么你需要`web.xml`.没办法解决它.但无论如何,大多数人应该使用3.x容器 (2认同)