我正在尝试验证对我的 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 …