如何使用注释在jackson的反序列化过程中强制执行A​​CCEPT_SINGLE_VALUE_AS_ARRAY

Vah*_*hid 15 java spring json jackson

有没有办法在类的List属性上使用注释在Jackson中使用ACCEPT_SINGLE_VALUE_AS_ARRAY?我正在使用Spring并获得以下异常

嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法从VALUE_STRING标记中反序列化java.util.ArrayList的实例

假设我有一个如下课程:

public class MyClass {

    private List < String > value;
}
Run Code Online (Sandbox Code Playgroud)

我的JSON结构如下:

情况1:

[{"operator": "in", "value": ["Active"], "property": "status"}]
Run Code Online (Sandbox Code Playgroud)

案例2:

[{"operator": "like", "value": "aba", "property": "desc"}]
Run Code Online (Sandbox Code Playgroud)

我应该使用什么注释来让框架知道我希望在反序列化时对这2个案例进行相同的处理

更新: 为了更加清晰,我在本文中将更新移至答案.

Öme*_*den 28

使用@JsonFormat注释:

public class MyClass {

    @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
    private List < String > value;

}
Run Code Online (Sandbox Code Playgroud)

您可以从这里查看其他选项版本

和要求(分钟2.7.0)

对于2.6.x

@Autowired private ObjectMapper mapper;
//...

mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Run Code Online (Sandbox Code Playgroud)

将此代码添加到初始化程序类中.

或者你可以直接在spring的bean配置中配置jackson.

这种配置可以解决问题,但是每次反序列化都会激活它.


Mic*_*ksa 6

我将创建一个自定义JsonDeserializer,我将处理这两种情况并使用反序列化器注释值类属性.

解串器:

public class StringListDeserializer extends JsonDeserializer<List<String>>{

    @Override
    public List<String> deserialize(JsonParser parser, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {

        List<String> ret = new ArrayList<>();

        ObjectCodec codec = parser.getCodec();
        TreeNode node = codec.readTree(parser);

        if (node.isArray()){
            for (JsonNode n : (ArrayNode)node){
                ret.add(n.asText());
            }
        } else if (node.isValueNode()){
            ret.add( ((JsonNode)node).asText() );
        }
        return ret;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的课:

public class MyClass {
    .
    @JsonDeserialize(using = StringListDeserializer.class)
    private List<String> value;
    .
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

顺便说一句:如果有一种方法只用注释处理这种情况,我也想知道它.


Vah*_*hid 6

为了清楚起见,我只是在回答自己的问题。答案之一是升级到更高版本,以便我可以使用注释。由于我的项目的依赖性限制,我无法做到这一点。

结果,基于Michal Foksa的答案,我通过创建自定义解串器解决了我的问题。其如下:

在我的财产上:

@JsonDeserialize(using = CustomStringDeserializer.class)
private List<String> value;
Run Code Online (Sandbox Code Playgroud)

还有我的反序列化器:

public class CustomStringDeserializer extends JsonDeserializer<List<String>>{

    @Override
    public List<String> deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature. ACCEPT_SINGLE_VALUE_AS_ARRAY);
        return mapper.readValue(p, List.class);
    }

}
Run Code Online (Sandbox Code Playgroud)


Suj*_*rla 5

对于春季项目添加

spring.jackson.deserialization.accept-single-value-as-array=true 
Run Code Online (Sandbox Code Playgroud)

属性文件中的内容适用于 spring 处理的所有反序列化,甚至适用@RequestBody于 API 的控制器类方法。