David Berg
David Berg

Reputation: 2058

How to prevent Jersey from parsing numeric fields to string when parsing to json?

I am building a Neo4J plugin using jersey-json to parse my POJO's to and from json. The problem is that my REST-services return numbers as strings.

If a POJO looks like this:

@XmlRootElement
public class Item {
    private Integer value = 5;
    private int primitiveValue = 6;

    public Item() {}

    public Integer getValue() {
        return value;
    }

    public int getPrimitiveValue() {
        return primitiveValue;
    }
}

I get a JSON-object looking like this:

{
    value: "5",
    primitiveValue: "6"
}

But I would expect it to be like this:

{
    value: 5,
    primitiveValue: 6
}

My resource looks like this:

@GET
@Path("/item")
@Produces(MediaType.APPLICATION_JSON)
public Response getItem() {
    return Response.ok(new Item()).build();
}

My pom looks like this:

<?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>my.project.id</groupId>
    <artifactId>neo4j-extension</artifactId>
    <version>1.0</version>

    <properties>
        <neo4j.version>2.2.0</neo4j.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j</artifactId>
            <version>${neo4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>server-api</artifactId>
            <version>${neo4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.neo4j.app</groupId>
            <artifactId>neo4j-server</artifactId>
            <version>${neo4j.version}</version>
        </dependency>

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

    </dependencies>

</project>

Do you know how I could solve this problem? Why is the default behavior to parse numeric fields as strings?

Solution

After suggestion from Stefan Armbruster I added the following dependency to my pom, and all dependent jars to my /plugins folder.

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

And removed @XmlRootElement annotation from my POJO.

The difference from before is that I get all null-values in my response. With the @XmlRootElement annotation null-values are removed from the response, but I do not think this will be a problem.

Upvotes: 3

Views: 830

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39925

Try to change your JAX-RS method to:

@GET
@Path("/item")
@Produces(MediaType.APPLICATION_JSON)
public Item getItem() {
    return new Item();
}

If you get an error, amend http://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson http://mvnrepository.com/artifact/com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider/2.6.2 to your dependencies (and make sure that this jar is deployed to the plugins folder as well).

Upvotes: 1

Related Questions