-------------- Generic Type ------------------
public class Pair<KT, VT> {
private KT key;
private VT value;
public Pair() {
}
public Pair(KT key, VT value) {
this.key = key;
this.value = value;
}
}
------------- Restful API ------------
@Path("/test")
public class TestApi {
@GET
@Path("/query")
@Produces(MediaType.APPLICATION_JSON)
public Pair query(@DefaultValue("0") @QueryParam("key") int key) {
return new Pair(key, "value:" + key);
}
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public String create(Pair<Integer, String> pair){
return "put success : " + pair.toString();
}
}
-------------问题--------------
GET的回应:http://localhost:8080/api/test/query是:
{"key":{"type":"int","value":0},"value":{"type":"string","value":"value:0"}}
但是,当我将相同的json发布到POST:http://localhost:8080/api/test/post时使用&#34; Content-Type:application / json&#34;标头,发生异常:
有谁能告诉我如何发布泛型类型的对象?
---------------的pom.xml ------------------
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.23.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>9.0.6.v20130930</jetty.version>
</properties>
答案 0 :(得分:1)
这不应该是对GET请求的正确回应
SELECT class,
COUNT(class) as N,
publisher
FROM books
WHERE publisher in (
'Company ABC', 'Company DEF', 'Company GHI',
'Company JKL', 'Company MNO'
)
GROUP BY class, publisher
;
应该是
{"key":{"type":"int","value":0},"value":{"type":"string","value":"value:0"}}
这只是使用MOXy的许多危险之一,Jackson是Jersey的默认JSON提供程序(在Glassfish中)。如果您使用ServerProperties.MOXY_JSON_FEATURE_DISABLE
,则应该看到正确的行为。
要使用Jackson,您可以通过将属性TableRow division设置为{"key":0,"value":"value:0"}
来禁用MOXy(可防止Jackson注册)。或者您可以添加The Jackson依赖项,然后在应用程序中注册true
。
JacksonFeature
进行更改后,它应该适用于GET和POST请求。
测试来源。运行测试时,您还应该在日志中看到请求的正确输出,该输出只返回与发布的相同的对。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey2.version}</version>
</dependency>