【发布时间】:2021-04-20 03:08:42
【问题描述】:
我正在尝试验证对我的 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;
}
另外,控制器上有一个有效的检查器:
@PostMapping
public EmployeeDTO store(@RequestBody @Valid EmployeeDTOInput employeeDTOInput) {
// Controller logic
}
编写了一些测试,我发现如果我的 JSON 请求对象具有这种语法,并且它工作正常:
{
name: 12345,
salary: "30000.50"
}
有没有办法拒绝这种请求,我的意思是,根据实际的DTO属性只接受JSON属性类型的100%一致性,只接受浮点格式到salary,字符串格式到name而不是忽略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
还有一些关于 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;
}
但请求仍然有效。
有没有办法防止这种“错误”的反序列化并将其配置为抛出异常?
【问题讨论】:
-
你是如何在测试中发送请求的?你能添加你的测试代码吗?请评论spring-boot版本等
-
我正在使用 RestAssured 和 Matchers,测试本身没有按预期通过,但我在测试中没有问题,问题当前在 POST 请求上,即允许应该被服务器解释为错误的主体类型,我可以使用任何 REST 客户端重现它。使用 Spring Boot 2.4.1