Jersey将数字转换为String

时间:2016-06-07 12:12:35

标签: java jersey jax-rs marshalling unmarshalling

我正在使用带有jeresy的码头服务器。 当预期的类型(代码中的 Pojo )是对象(/ 可序列化)。 Jersey将数字转换为com.sun.org.apache.xerces.internal.dom.ElementNSImpl类型(或者表示我正在使用Serializable时表示数字的字符串)。

我错过了什么吗?

资源:

@Path("/entry-point")
public class EntryPoint {

    @POST
    @Consumes("application/json")
    @Produces("application/json")
    public Pojo post(Pojo pojo) {
        return pojo;
    }
}

对象:

public class Pojo implements Serializable{
    public int i;
    public Object object;
}

Jetty服务器:

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");

    Server jettyServer = new Server(8080);
    jettyServer.setHandler(context);


    ServletHolder jerseyServlet = context.addServlet(
            org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);

    // Tells the Jersey Servlet which REST service/class to load.
    jerseyServlet.setInitParameter(
            "jersey.config.server.provider.classnames",
            EntryPoint.class.getCanonicalName());

    try {
        jettyServer.start();
        jettyServer.join();
    } finally {
        jettyServer.destroy();
    }

和pom.Xml:

    <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>

1 个答案:

答案 0 :(得分:0)

您应该在Pojo中使用java.lang.Number而不是Object来存储数值。