- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用以下方法的 JAX-RS WebService:
@Path("/myrest")
public class MyRestResource {
...
@GET
@Path("/getInteger")
@Produces(APPLICATION_JSON)
public Integer getInteger() {
return 42;
}
使用此片段访问时:
@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 注释的答案在这里都不适用。或者如果我无权访问它,我应该使用哪个注释来描述返回类型类源?
使用 ngrep 我可以验证网络服务是否仅返回字符串“42”。根据规范,这是一个有效的 JSON“值”,但不是有效的 JSON“文本”。那么我的问题是在客户端还是服务器端?
我尝试根据 http://tugdualgrall.blogspot.de/2011/09/jax-rs-jersey-and-single-element-arrays.html 激活 JSONConfiguration natural/badgerfish但没有成功(ngrep 仍然只显示“42”)。那是正确的道路吗?
欢迎任何想法!
最佳答案
这是一个 recognized bug in Jackson ,这已被吹捧为(我认为是错误的)功能。为什么我认为这是一个错误?因为虽然序列化有效,但反序列化肯定无效。
无论如何,无法从您当前的返回类型生成有效的 JSON,因此我建议创建一个包装类:
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 对象中,由objects simple type name - 但请注意,如果使用此选项,所有生成的 JSON 都将被包装(不仅仅是原始类型):
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);
输出:
{"Integer":42}
Deserialized Value: 42
参见 this answer有关如何在 JAX-RS 环境中检索和配置 ObjectMapper
实例的详细信息。
关于json - JAX-RS/Jersey ".get(Integer.class)"和单个 JSON 原语(整数)值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16408950/
我正在尝试执行 JavaPairRDD 和 JavaPairRDD 的 leftOuterJoin> 并且函数签名返回类型是 JavaPairRDD>>> 这里可选的是 com.google.comm
我正在尝试按元素的频率对元素进行排序 import java.io.BufferedReader; import java.io.IOException; import java.io.InputSt
这个问题已经有答案了: Is List a subclass of List? Why are Java generics not implicitly polymorphic? (19 个回答) 已
编辑:问题已解决:请参阅 Karim SNOUSSI 的答案和我在下面的评论。 这是我在堆栈溢出时遇到的第一个问题,所以我可能不会一开始就把所有事情都做对。对此感到抱歉。此外,我对 Java 和一般
#include #include using namespace std; class Integer { public: int i; Integer (int ll
我不明白: ArrayList list = new ArrayList(); Collection list1 = new ArrayList(); 类 ArrayList扩展实现接口(interf
我编写了:。它成功了。我不知道为什么?
我编写了:。它成功了。我不知道为什么
我编写了:。它成功了。我不知道为什么?
Collectors.counting()返回 long此方法中每个键的值: private static Map countDuplicates(HashSet cards) { retur
我正在尝试通过搜索旧元素并将其替换为新元素来更新节点的元素。但是有一个我不明白的错误。是什么导致我的代码出现该错误,我该如何解决?错误; The method update(Integer, Inte
我有一个称为 client 的表,其中有一列称为created_time ,所以实际上我想绘制一个 map ,以便我可以知道在哪一年和哪一个月添加了多少客户?现在的要求是假设在 2018 年 11 月
这个问题已经有答案了: Is Java "pass-by-reference" or "pass-by-value"? (91 个回答) 已关闭 8 年前。 我对 ArrayList Collecti
我意识到下面的代码是正确的 Integer.MIN_VALUE == -Integer.MIN_VALUE == Math.abs(Integer.MIN_VALUE) 这是因为当我们取反-21474
我有以下类 AccountWebappGridRow,它扩展了 AccountGridRow: public class AccountWebappGridRow extends AccountGri
我正在学习 Haskell 并看到了函数组合。 尝试复合 map和 foldl mapd = (map.foldl) 比 test = (mapd (\x y -> x + y ) [1,2,3,4]
我有两个相同大小的数组和两个方法。 public class Client { private static int[] ints; private static final int
我喜欢 Java 8 中的 Streams 概念。现在我想借助 Java Streams 将 Java 中的 Map 转换为排序列表。我只想显示列表而不将其存储在任何地方。我希望在结果列表中有这个输出
我有一个数据库表,其中包含电视节目类型列表和关联的 ARGB 颜色值,用于在显示电视指南时突出显示 Android ListView 中的电视节目。流派表看起来像这样... id genre
我有一个 Integer 类,它应该模拟一个整数 mod n。因此,它具有如下构造函数: Integer::Integer(int x) : m(x), n(0) { } Integer::I
我是一名优秀的程序员,十分优秀!