作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在与 Jackson 进行一个非常简单的测试。我有一个类,并使用它的对象作为 Jersey 方法的参数和返回值。类(class)是:
import java.util.List;
public class TestJsonArray {
private List<String> testString;
public List<String> getTestString() {
return testString;
}
public void setTestString(List<String> testString) {
this.testString = testString;
}
}
我有一个 Jersey 方法,尝试将一个字符串添加到列表测试字符串
@Path("/arrayObj")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Object createObjectArray(@QueryParam("param") String object) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
TestJsonArray convertValue = objectMapper.convertValue(object, TestJsonArray.class);
convertValue.getTestString().add("hello");
return objectMapper.writeValueAsString(convertValue);
}
当我使用参数调用此方法时
{"testString":["Hi"]}
我遇到异常:
java.lang.IllegalArgumentException: Can not construct instance of test.rest.TestJsonArray, problem: no suitable creator method found to deserialize from JSON String
at [Source: N/A; line: -1, column: -1]
反序列化过程中抛出异常:
TestJsonArray convertValue = objectMapper.convertValue(object, TestJsonArray.class);
我想知道为什么会抛出这个异常。我做错了什么?
最佳答案
尝试使用ObjectMapper
的readValue
方法而不是convertValue
objectMapper.readValue(json, TestJsonArray.class);
这应该有效。
关于java - 使用 Jackson 将 JSON 反序列化为包含集合的 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16605490/
出于好奇,我尝试了一些原型(prototype)制作,但似乎只允许在第一个位置使用子例程的原型(prototype) &。 当我写作时 sub test (&$$) { do_somethin
我需要开发一个类似于 Android Play 商店应用程序或类似 this app 的应用程序.我阅读了很多教程,发现几乎每个教程都有与 this one 类似的例子。 . 我已经开始使用我的应用程
考虑一个表示“事件之间的时间”的列: (5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2) 我想将这些分组到 30 个桶中,但桶会重置。期望的结果: (0, 1,
我是一名优秀的程序员,十分优秀!