Enforce not-null field in JSON object

后端 未结 4 809
余生分开走
余生分开走 2020-12-10 00:43

Our REST API receives some JSON objects input where some fields are required to be not null. Those can be either String/Integer or even might be some other class instance as

4条回答
  •  没有蜡笔的小新
    2020-12-10 01:15

    You can enforce not null validation using a combination of the Jackson JSON library and the javax.validation together with the Hibernate validator package.

    If you are using Maven these are the dependencies you can use:

    
        
            com.fasterxml.jackson.core
            jackson-core
            ${jackson-version}
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            ${jackson-version}
        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson-version}
        
        
        
            javax.validation
            validation-api
            2.0.1.Final
        
    
        
            org.hibernate.validator
            hibernate-validator
            ${hibernate-validator.version}
        
        
            org.hibernate.validator
            hibernate-validator-annotation-processor
            ${hibernate-validator.version}
        
    
        
            javax.el
            javax.el-api
            3.0.0
        
    
        
            org.glassfish.web
            javax.el
            2.2.6
        
    
    
    

    Then your code will have to convert some JSON into your annotated object and you will need to validate the object using javax.validation.Validator. Here is some sample code demonstrating how this can be done (see the relevant validate method):

    public class ShareLocationService {
    
        private ObjectMapper mapper = new ObjectMapper();
    
        private ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    
        // Materialize the Java object which contains the validation annotations
        public ShareLocation readFrom(String json) throws IOException {
            return mapper.readerFor(ShareLocation.class).readValue(json);
        }
    
        // validate and collect the set of validations
        public Set validate(String json) throws IOException {
            ShareLocation shareMyLocation = readFrom(json);
            Validator validator = factory.getValidator();
            Set> violations = validator.validate(shareMyLocation);
            return violations.stream().map(Object::toString).collect(Collectors.toSet());
        }
    }
    

    Here is a sample class using the validation annotations:

    public class ShareLocation {
        @JsonProperty("Source")
        @NotNull
        private String source;
        @JsonProperty("CompanyCode")
        private String companyCode;
        @JsonProperty("FirstName")
        private String firstName;
        @JsonProperty("LastName")
        private String lastName;
        @JsonProperty("Email")
        private String email;
        @JsonProperty("MobileNumber")
        private String mobileNumber;
        @JsonProperty("Latitude")
        @NotNull
        private Double latitude;
        @JsonProperty("Longitude")
        @NotNull
        private Double longitude;
        @JsonProperty("LocationDateTime")
        @NotNull
        private String locationDateTime;
    

提交回复
热议问题