给定以下Spring Boot(v 3.0.2)控制器操作
@PutMapping("/api/thing/{thingId}") @ResponseStatus(HttpStatus.OK) public void updateThing( @PathVariable String thingId, @Valid @RequestBody ThingRequest request) { thingService.updateThing(thingId, request); }
请求主体绑定到以下类的实例
public record ThingRequest( @Size(min = 10, max = 2500) @NotBlank String name) { }
如果用户提交的名称为" my thing "
,验证将通过,因为有10个字符(包括空格)。我希望在验证之前将字符串修剪为"my thing"
。
我可以自己在ThingRequest
的构造函数中实现这一点,但我怀疑有一种更具声明性的方法。理想情况下,我希望将此修剪应用于
- 用
@RequestBody
- 注释的对象的所有字符串财产用
@RequestPart
- 注释的对象所有字符串财产用
@PathVariable
注释的所有字符串
但目前我只需要(1)。Spring Boot是否提供了自动执行此请求数据修剪的方法?我测试了这个答案中建议的方法,但没有奏效。