pok*_*110 0 java json arraylist jackson deserialization
我正在和杰克逊做一个非常简单的测试。我有一个类并将其对象用作 Jersey 方法的参数和返回值。班级是:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个 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);
}
Run Code Online (Sandbox Code Playgroud)
当我用参数调用这个方法时
{"testString":["嗨"]}
我得到一个例外:
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]
Run Code Online (Sandbox Code Playgroud)
在反序列化过程中抛出异常:
TestJsonArray convertValue = objectMapper.convertValue(object, TestJsonArray.class);
我想知道为什么会抛出这个异常。我究竟做错了什么?
尝试readValue方法ObjectMapper而不是convertValue
objectMapper.readValue(json, TestJsonArray.class);
Run Code Online (Sandbox Code Playgroud)
这应该有效。
| 归档时间: |
|
| 查看次数: |
8383 次 |
| 最近记录: |