Ric*_*ard 13 java spring json spring-mvc jackson
我有一个springboot休息服务.用户传入一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
所以用户需要传入以下json:
{
"id": “123457896”,
"code": "Foo",
"name": "test"
}
Run Code Online (Sandbox Code Playgroud)
我想要完成所有这些领域.提供更少或更多的东西会引发异常.有没有办法告诉杰克逊在反序列化时验证输入?我试过@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.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,检查每个领域似乎都很乏味和丑陋.所以鉴于我使用的是java 8,spring boot和jackson的最新版本,在验证传入的json输入方面,最佳做法是什么?或者我是否已经以最新的方式做到了?
var*_*ren 15
有没有必要自定义的验证.有办法告诉杰克逊扔
JsonMappingException 如果您没有必填字段
UnrecognizedPropertyException如果你有额外的字段(UnrecognizedPropertyException只是扩展JsonMappingException).
您只需要添加@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;
}
Run Code Online (Sandbox Code Playgroud)
完整演示:
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 + '\'' +
'}';
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
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"])
Run Code Online (Sandbox Code Playgroud)
小智 5
在构造函数上使用@JsonCreator注释与 一起@JsonProperty使用将起作用。也检查这个答案。
public final class Request {
private String id;
private double code;
private String name;
@JsonCreator
public Request(@JsonProperty(required = true) String id,
@JsonProperty(required = true) double code,
@JsonProperty(required = true) String name) {
this.id = id;
this.code = code;
this.name = name;
}
public String getId() {
return id;
}
public double getCode() {
return code;
}
public String getName() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)