Jackson Serializer没有调用Jersey Jax-RS

时间:2017-03-29 11:16:10

标签: java jersey jackson glassfish jax-rs

开发JAX-RS应用程序并解决问题。 需要从我的资源中自定义json-output如此配置Jersey(2.22.2)以使用Jackson(2.5)解析器而不是默认Moxy(根据this answer)。

这是pom.xml片段

        <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.25.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.22.2</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

我还将web.xml文件配置为默认使用Jackson

    <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>
                com.mycompany.myresourcepackage
                com.fasterxml.jackson.jaxrs
            </param-value>
        </init-param>

但资源输出不受我使用注释和序列化程序配置的影响。 这是表示为json的模型

    @XmlRootElement(name = "geo")
public class Geometry {
    public Geometry() { 
        coordinates = new ArrayList<List<Double>>();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @JsonSerialize(using = CoordinatesSerializer.class)
    public List<List<Double>> getCoordinates() {
        return coordinates;
    }

    public void setCoordinates( List<List<Double>> coordinates) {
        this.coordinates = coordinates;
    }

    @JsonProperty("tp")
    private String  type;
    private List<List<Double>> coordinates;
}

和序列化器

protected CoordinatesSerializer(Class<List<List<Double>>> t) { }

private static final long serialVersionUID = 1L;

@Override
public void serialize(List<List<Double>> value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException {
    try {
        jgen.writeArrayFieldStart("motherfucking-coordinates");

        int coordinates_size = value.size();
        for (int i = 0; i < coordinates_size; i++) {
            jgen.writeStartArray();
            jgen.writeNumber(value.get(i).get(0));
            jgen.writeNumber(value.get(i).get(1));
            jgen.writeEndArray();
        }

        jgen.writeEndArray();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

}

这是资源片段

 @GET
    @Path("/route/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Geometry(@PathParam("id") int id) {
    // construct and return object
}

响应json根本不是自定义的。

{"geometry":{"coordinates":["27.56 53.9","27.58 53.88","27.55 53.94"],"type":"LineString"},"id":"1","type":"Feature"}

所需的输出

{"geometry":{"coordinates":[[27.56, 53.9],[27.58, 53.88],[27.55, 53.94]],"type":"LineString"},"id":"1","type":"Feature"}

非常感谢。

1 个答案:

答案 0 :(得分:1)

我终于设法将Jersey项目配置为使用Jackson JSON解析器而不是Moxy(默认)。该应用程序在Glassfish JEE Server上运行。

Maven依赖

    <dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.5.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.3</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

您需要使用extends org.glassfish.jersey.server.ResourceConfig创建类。首次加载应用时会创建该类的对象。

public class MyApplication extends ResourceConfig {
public MyApplication() {
    // create custom ObjectMapper
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    // create JsonProvider to provide custom ObjectMapper
    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    provider.setMapper(mapper);

    register(provider);
    register(JacksonFeature.class);
    packages("your.resources.package");
}
}

创建的MyApplication类应该在web.xml文件中注册(实际创建)。编辑web.xml的部分。

<servlet>
    <servlet-name>App</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>your.app.package.MyApplication</param-value>

    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

列出的步骤足以将Jackson JSON Parser注册为Jersey Jax-RS app的默认值。