Java 8 杰克逊验证

2022-09-01 17:07:49

我有一个弹簧靴休息服务。用户传入一个 json 对象,该对象被反序列化为以下 java pojo:

public final class Request {
    private String id;
    private double code;
    private String name;

    public String getId() {
        return id;
    }

    public double getCode() {
        return code;
    }

    public String getName() {
        return name;
    }
}

因此,用户需要传入以下 json:

{
    "id": “123457896”,
    "code": "Foo",
    "name": "test"
} 

我想将所有这些字段设为必填字段。提供任何少或多的内容将引发异常。有没有办法告诉 Jackson 在反序列化时验证输入?我试过,但这不起作用;显然,从这里和这里来看,注释似乎没有得到尊重。@JsonProperty(required=true)JsonProperty

我有这个验证器,我在我的控制器中调用:

@Component
public class RequestValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return false;
    }

    @Override
    public void validate(Object target, Errors errors) {
        String id = ((Request) target).getId();
        if(id == null || id.isEmpty()) {
            throw new InvalidRequestException("A valid id is missing. Please provide a non-empty or non-null id.");
        }
    }
}

但是,检查每个字段似乎既乏味又丑陋。因此,鉴于我使用的是java 8,spring boot和最新版本的jackson,在验证传入的json输入方面,最佳实践是什么?还是我已经以最新的方式做到了这一点?


答案 1

不需要自定义验证器。有一种方法可以告诉杰克逊投掷

  • JsonMappingException如果您没有必填字段

  • UnrecognizedPropertyException如果你有额外的字段(只是扩展的)。UnrecognizedPropertyExceptionJsonMappingException

您只需要添加或自定义构造函数。像这样的东西应该工作:@JsonCreator

public Request(@JsonProperty(value= "id", required = true)String id,
               @JsonProperty(value= "code",required = true)double code,
               @JsonProperty(value= "name",required = true)String name) {
    this.id = id;
    this.code = code;
    this.name = name;
}

完整演示:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException {
    test("{\"id\": \"123457896\",\"code\": 1,\"name\": \"test\"}");
    test("{\"id\": \"123457896\",\"name\": \"test\"}");
    test("{\"id\": \"123457896\",\"code\": 1, \"c\": 1,\"name\": \"test\"}");
}

public static void test(String json) throws IOException{
    ObjectMapper mapper = new ObjectMapper();
    try {
        Request deserialized = mapper.readValue(json, Request.class);
        System.out.println(deserialized);
        String serialized = mapper.writeValueAsString(deserialized);
        System.out.println(serialized);
    } catch (JsonMappingException e) {
        System.out.println(e.getMessage());
    }
}

public static class Request {
    private String id;
    private double code;
    private String name;

    public Request(@JsonProperty(value= "id", required = true)String id,
                   @JsonProperty(value= "code",required = true)double code,
                   @JsonProperty(value= "name",required = true)String name) {
        this.id = id;
        this.code = code;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getCode() {
        return code;
    }

    public void setCode(double code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Request{" +
                "id='" + id + '\'' +
                ", code=" + code +
                ", name='" + name + '\'' +
                '}';
    }
}
}

结果:

Request{id='123457896', code=1.0, name='test'}
{"id":"123457896","code":1.0,"name":"test"}
Missing required creator property 'code' (index 1)
 at [Source: {"id": "123457896","name": "test"}; line: 1, column: 34]
Unrecognized field "c" (class Main7$Request), not marked as ignorable (3 known properties: "id", "code", "name"])
 at [Source: {"id": "123457896","code": 1, "c": 1,"name": "test"}; line: 1, column: 53] (through reference chain: Request["c"])

答案 2

您使用了 Spring Validator 方法。还有另一种方法:

J2EE JSR-303/JSR-349 Bean Validation API.它提供验证注释(javax,而不是jackson)。

在这里查看两者的好例子