Jackson Mapper-如何在空值或空值上失败



我们在代码中使用Jackson JSON Mapper来删除某些配置对象。我们希望杰克逊在缺少特定字段或空的

时将杰克逊失败。

杰克逊支持此行为的唯一功能是针对原语:

final DeserializationConfig.Feature failOnPremitives = DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES;

问题是所讨论的字段主要是字符串

任何帮助都得到高度赞赏

有一个名为: FAIL_ON_NULL_FOR_CREATOR_PARAMETERS

的选项

所以我认为可以在以下方面访问: DeserializationConfig.Feature.FAIL_ON_NULL_FOR_CREATOR_PARAMETERS;

yml

jackson:
    serialization:
      indent-output: false
    deserialization:
      fail-on-unknown-properties: true
      fail-on-missing-creator-properties: true
      fail-on-null-creator-properties: true

这对所有类型,字符串,INT,双打等都有效。

您是否考虑过bean验证?

杰克逊专注于JSON解析时,Bean验证都是关于在您的豆上声明和执行验证。

您可以从Hibernate验证器(BEAN验证参考实现)中使用@NotNull@NotBlank


另外,您可以使用JSON模式。

必须创建特定的自定义求职者。

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.IOException;
class JacksonDeserializerTest {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule("CustomPersonDeserializer", new Version(1, 0, 0, null, null, null));
        module.addDeserializer(Person.class, new CustomPersonDeserializer());
        mapper.registerModule(module);
        String jsonString = "{ "id": 1, "name": "User 1 "}";
        Person user = mapper.readValue(jsonString, Person.class);
        System.out.println("User: " + user.toString());
        jsonString = "{ "id": 1}";
        user = mapper.readValue(jsonString, Person.class);
    }
    static class CustomPersonDeserializer extends StdDeserializer<Person> {
        private static final long serialVersionUID = -4100181951833318756L;
        public CustomPersonDeserializer() {
            this(null);
        }
        public CustomPersonDeserializer(Class<?> vc) {
            super(vc);
        }
        @Override
        public Person deserialize(JsonParser parser, DeserializationContext deserializer) throws IOException, JsonProcessingException {
            Person person = new Person();
            ObjectCodec codec = parser.getCodec();
            JsonNode node = codec.readTree(parser);
            JsonNode idNode = node.get("id");
            int id = idNode.asInt();
            person.setId(id);
            JsonNode nameNode = node.get("name");
            if(nameNode == null){
                throw new IOException("name must be provided");
            }
            String name = nameNode.asText();
            if (name.trim().length() < 1){
                throw new IOException("name can not be empty");
            }
            person.setName(name);
            return person;
        }
    }
    static class Person {
        private int id;
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + ''' +
                    ", id=" + id +
                    '}';
        }
    }
}

替代方式:
这也适用于对象

ObjectMapper mapper= new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

相关内容

最新更新