vin*_*lix 0 java spring json jackson
我正在尝试验证对我的 REST 控制器的 POST 请求,在我的 DTO 类上有一些属性和验证:
EmployeeDTOInput.java
@Getter
@Setter
public class EmployeeDTOInput {
@NotBlank("name must not be blank!")
private String name;
@DecimalMin(value = "0.01", message = "salary must be greather than or equal to $0.01!")
private BigDecimal salary;
@NotNull("commission elegible must not be null!")
private boolean commissionElegible;
}
Run Code Online (Sandbox Code Playgroud)
另外,在控制器上有一个有效的检查器:
@PostMapping
public EmployeeDTO store(@RequestBody @Valid EmployeeDTOInput employeeDTOInput) {
// Controller logic
}
Run Code Online (Sandbox Code Playgroud)
写一些测试,我发现,如果我的 JSON 请求对象有这个语法,它工作正常:
{
name: 12345,
salary: "30000.50"
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以拒绝这种请求,我的意思是,根据实际的 DTO 属性仅接受 JSON 属性类型的 100% 一致性,仅接受浮点格式 to salary,字符串格式 toname并且不忽略commissionElegible?
我已经尝试在 application.properties 上添加一些 Jackson 属性:
spring.jackson.deserialization.fail-on-unknown-properties=true
spring.jackson.deserialization.fail-on-ignored-properties=true
spring.jackson.deserialization.fail-on-invalid-subtype=true
Run Code Online (Sandbox Code Playgroud)
还有一些关于 DTO 类的 Jackson 注释:
@Getter
@Setter
public class EmployeeDTOInput {
@JsonFormat(shape = JsonFormat.Shape.STRING)
@NotBlank("name must not be blank!")
private String name;
@JsonFormat(shape = JsonFormat.Shape.NUMBER_FLOAT)
@DecimalMin(value = "0.01", message = "salary must be greather than or equal to $0.01!")
private BigDecimal salary;
@JsonFormat(shape = JsonFormat.Shape.BOOLEAN)
@NotNull("commission elegible must not be null!")
private boolean commissionElegible;
}
Run Code Online (Sandbox Code Playgroud)
但请求仍然有效。
有什么方法可以防止这种“错误”的反序列化并将其配置为抛出异常?
小智 5
I believe you need to configure ALLOW_COERCION_OF_SCALARS property of ObjectMapper.
It can be achieved by changing setting in application.properties:
spring.jackson.mapper.allow-coercion-of-scalars=false
Run Code Online (Sandbox Code Playgroud)
And here is the JavaDoc:
| 归档时间: |
|
| 查看次数: |
258 次 |
| 最近记录: |