JAX-RS:Jersey + Genson = MessageBodyProviderNotFoundException

时间:2015-03-04 21:42:46

标签: java glassfish jersey jax-rs genson

我正在使用JAX-RS(Jersey)和Glassfish作为服务器开发RESTful Web服务。我实现了返回entites列表的简单方法,当我尝试访问它时,服务器丢失500错误

  

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:   找不到媒体类型= application / json。

的MessageBodyWriter

我尝试将Genson lib添加到我的项目中,但这没有用。我的代码出了什么问题?

我的实体

@XmlRootElement
public class Device {

    public Device(){}

    public Device(String vendor, String model) {
        this.vendor = vendor;
        this.model = model;
    }
    public String vendor;
    public String model;
}

服务

@Path("devices")
public class Devices {
    @Context
    Application app;
    @Context
    Providers providers;

    @GET
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response getAll(){
        List<Device> result = new ArrayList<>();
        result.add(new Device("Samsung","Galaxy S4"));
        return Response.ok().entity(result).build();
    }
}

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>Kontroller</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.drozdov.kontroller.api, com.drozdov.kontroller.models</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

</web-app>

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.drozdov</groupId>
    <artifactId>Kontroller</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.owlike</groupId>
            <artifactId>genson</artifactId>
            <version>0.99</version>
        </dependency>
    </dependencies>
</project>

1 个答案:

答案 0 :(得分:1)

你在使用泽西2吗?如果是,这可能是它不起作用的原因。尝试使用最新的Genson版本。它应该与泽西2 +开箱即用。

<dependency>
  <groupId>com.owlike</groupId>
  <artifactId>genson</artifactId>
  <version>1.2</version>
</dependency>