杰克逊:如何将字符串读作列表?
Jackson: how to read a String as List?
我们目前的 class 看起来像
public class Attributes {
private String mapping;
.....
和
{
"mapping": "displayName",
.....
效果很好,已发货给客户。
我们将JSON
转换为Attribute
class的方式是
JSON.readValue(jsonFile, Attribute.class);
最近要求说,mapping
将是 List<String>
而不是 String
起初,我想到的快速更改是将 mapping
更改为 List<String>
,但这会破坏现有客户。
我通过编写
的测试来尝试
assertEquals("displayName", configuration.getMapping().get(0));
它失败了
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
问题
我如何告诉 Jackson
将 String
读取为 `List?它将是 1 项列表,但会向后兼容。
谢谢
答案是Can not deserialize instance of java.util.ArrayList out of VALUE_STRING
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
我们目前的 class 看起来像
public class Attributes {
private String mapping;
.....
和
{
"mapping": "displayName",
.....
效果很好,已发货给客户。
我们将JSON
转换为Attribute
class的方式是
JSON.readValue(jsonFile, Attribute.class);
最近要求说,mapping
将是 List<String>
而不是 String
起初,我想到的快速更改是将 mapping
更改为 List<String>
,但这会破坏现有客户。
我通过编写
的测试来尝试assertEquals("displayName", configuration.getMapping().get(0));
它失败了
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
问题
我如何告诉 Jackson
将 String
读取为 `List?它将是 1 项列表,但会向后兼容。
谢谢
答案是Can not deserialize instance of java.util.ArrayList out of VALUE_STRING
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);