JAX-RS/球衣".get(Integer.class)"和单个JSON原语(整数)值? [英] JAX-RS / Jersey ".get(Integer.class)" and single JSON primitive (Integer) values?
问题描述
我具有使用以下方法的JAX-RS WebService:
I have a JAX-RS WebService with the following method:
@Path("/myrest")
public class MyRestResource {
...
@GET
@Path("/getInteger")
@Produces(APPLICATION_JSON)
public Integer getInteger() {
return 42;
}
使用此摘录进行访问时:
When accessed using this snipped:
@Test
public void testGetPrimitiveWrapers() throws IOException {
// this works:
assertEquals(new Integer(42), new ObjectMapper().readValue("42", Integer.class));
// that fails:
assertEquals(new Integer(42), resource().path("/myrest/getInteger").get(Integer.class));
}
我得到以下异常:
com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: A message body reader for Java class java.lang.Integer, and Java type class java.lang.Integer, and MIME media type application/json was not found
com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type are: application/json
...
问题仅在于返回单个原始值(int/boolean)或它们的包装器类.返回其他POJO类不是问题,因此我猜想有关JSONConfiguration.FEATURE_POJO_MAPPING和JAXB注释的所有答案都不适用于这里. 或者,如果我无权访问返回值,应该使用哪种注释来描述返回类型 课源?
The problem is just with returning single primitive values (int/boolean) or their wrapper classes. Returning other POJO classes is not the problemen so I guess all the answers regarding JSONConfiguration.FEATURE_POJO_MAPPING and JAXB annotations do not apply here. Or which annotation should I use to describe the return type if I don't have access to its class source?
使用ngrep,我可以验证Web服务仅返回字符串"42".根据规范,这就是有效的JSON值",但不是有效的JSON文本".那我是在客户端还是在服务器端呢?
Using ngrep I can verify that just the String "42" is returned by the webservice. Thats a valid JSON "value" but not a valid JSON "text" according to the spec. So is my problem on the client or the server side?
我尝试根据 http://tugdualgrall.blogspot.de/2011/09/jax-rs-jersey-and-single-element-arrays.html ,但没有成功(ngrep仍然仅显示"42").那是正确的道路吗?
I tried activating JSONConfiguration natural/badgerfish according to http://tugdualgrall.blogspot.de/2011/09/jax-rs-jersey-and-single-element-arrays.html but with no success (ngrep still shows just "42"). Would that be the right path?
任何想法都值得赞赏!
推荐答案
这是认识到Jackson中的错误,该错误已被吹捧为功能(在我看来是不正确的).为什么我认为它是一个错误?因为在进行序列化时,反序列化绝对不会.
This is a recognized bug in Jackson, which has been touted (incorrectly in my opinion) as a feature. Why do I consider it a bug? Because while serialization works, deserialization definitely does not.
在任何情况下,都无法从您当前的返回类型生成有效的JSON,因此,我建议创建一个包装器类:
In any case, valid JSON cannot be generated from your current return type, so I would recommend creating a wrapper class:
class Result<T> {
private T data;
// constructors, getters, setters
}
@GET
@Path("/getInteger")
@Produces(APPLICATION_JSON)
public Result<Integer> getInteger() {
return new Result<Integer)(42);
}
或者 ,您可以 选择包装根值,这将自动将您的数据封装在顶级JSON对象中,并以对象的简单类型名称-但请注意,如果使用此选项,则将包装所有生成的JSON(而不仅仅是原始类型):
Alternatively, you can elect to wrap root values, which will automatically encapsulate your data in a top level JSON object, keyed by the objects simple type name - but note that if this option is used that all generated JSON will be wrapped (not just for primitives):
final ObjectMapper mapper = new ObjectMapper()
.configure(SerializationFeature.WRAP_ROOT_VALUE, true)
.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
final String serializedJson = mapper.writeValueAsString(42);
final Integer deserializedVal = mapper.readValue(serializedJson,
Integer.class);
System.out.println(serializedJson);
System.out.println("Deserialized Value: " + deserializedVal);
输出:
{整数":42}
反序列化值:42
{"Integer":42}
Deserialized Value: 42
有关如何在JAX-RS环境中检索和配置ObjectMapper
实例的详细信息,请参见此答案.
See this answer for details on how to retrieve and configure your ObjectMapper
instance in a JAX-RS environment.
这篇关于JAX-RS/球衣".get(Integer.class)"和单个JSON原语(整数)值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!