我正在使用 Jeresy 的 jetty 服务器。 当预期类型(代码中的 Pojo)为 Object (/Serialized)时,我尝试发布带有整数/浮点值的 json。 Jersey 将数字转换为 com.sun.org.apache.xerces.internal.dom.ElementNSImpl 类型(如果我使用 Serialized,则转换为表示数字的字符串)。
我错过了什么吗?
资源:
@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>
最佳答案
您应该在 Pojo 中使用 java.lang.Number 而不是 Object 来存储数值。
关于java - Jersey 将数字转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37679059/