我正在使用带有挑逗性的码头服务器。
当期望的类型(代码中的Pojo)为Object(/ Serializable)时,我尝试发布具有整数/浮点值的json。
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;
}
码头服务器:
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>
最佳答案
您应该在Pojo中使用java.lang.Number而不是Object来存储数字值。