使用Jackson JSON库和Jersey + Jetty

时间:2015-09-14 14:03:40

标签: java json rest jersey jackson

我使用嵌入式 Jetty + Jackson 实施了 Jersey 服务。虽然当我尝试使用GET资源(不消耗或生成JSON)时,它可以工作,但它不适用于POST资源。控制台上的消息显示为:

SEVERE: MessageBodyReader not found for media type=application/json, type=class com.delta.model.EnableDisableMessage, genericType=class com.delta.model.EnableDisableMessage

POST资源:

@POST
@Path("/enable")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String enablePost(EnableDisableMessage enable) throws InterruptedException {     
    logger.debug("within Proxy POST method..." + enable.toString());        
    return "it worked!";
}

模特:

package com.delta.model;

public class EnableDisableMessage {

    private String cell;
    private String instruction;


    public EnableDisableMessage(String cell, String instruction) {
        super();
        this.cell = cell;
        this.instruction = instruction;
    }

    public String getCell() {
        return cell;
    }

    public void setCell(String cell) {
        this.cell = cell;
    }

    public String getInstruction() {
        return instruction;
    }

    public void setInstruction(String instruction) {
        this.instruction = instruction;
    }

}

我还实现了这个配置类,详见Jersey规范:

@ApplicationPath("/")
public class MyApplication extends ResourceConfig {

    public MyApplication() {        
        packages("com.delta.model;com.delta.rest");
        register(JacksonFeature.class);
//      property(CommonProperties.MOXY_JSON_FEATURE_DISABLE, true);      
    }
}

pom.xml的依赖项:

<dependencies>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.2.3.v20140905</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>9.2.3.v20140905</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-http</artifactId>
            <version>2.7</version>
        </dependency>

        <!--  
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.7</version>
        </dependency>
        -->

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

    </dependencies>

1 个答案:

答案 0 :(得分:3)

在您的依赖项中包含Jackson JSON-provider可能有帮助吗?

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.6.1</version>
</dependency>