问题示例
@RepositoryRestController
@RequestMapping("/root")
public class DeviceScheduleController {
@PostMapping("/test")
public ResponseEntity<?> test(@Valid @RequestBody TestDTO testDTO) {
// service details
return ResponseEntity.ok();
}
}
问题原因
原因是@Valid仅在Controller类被@RestController或@Controller注解时才生效,当Controller类被@RepositoryRestController注解时将不会生效。
解决方法
给Controller类加入注解@Validated:
@Validated
@RepositoryRestController
@RequestMapping("/root")
public class DeviceScheduleController {
@PostMapping("/test")
public ResponseEntity<?> test(@Valid @RequestBody TestDTO testDTO) {
// service details
return ResponseEntity.ok();
}
}
参考链接
https://coderanch.com/t/724074/frameworks/Valid-annotation-working-service-method