要使其正常工作,您必须执行以下操作:
将
MethodValidationPostProcessorbean 添加到配置
@Beanpublic MethodValidationPostProcessor methodValidationPostProcessor() { return new MethodValidationPostProcessor();}添加
@Validated到您的EmployeeController
@Validated@RestControllerpublic class EmployeeController {}'添加
@Valid到Map或Employee
public List<Employee> newEmployee(@RequestBody @Valid Map<String, Employee> newEmployees) {} public List<Employee> newEmployee(@RequestBody Map<String, @Valid Employee> newEmployees) {}就这样。这是整个过程
EmployeeController:
@Validated@RestControllerpublic class EmployeeController { @PostMapping("/employees") public List<Employee> newEmployee(@RequestBody @Valid Employee newEmployee) { return Collections.singletonList(newEmployee); } @PostMapping("/employees/bulk") public List<Employee> newEmployee(@RequestBody @Valid Map<String, Employee> newEmployees) { return new ArrayList<>(newEmployees.values()); }}和SpringBoot的配置文件
@SpringBootApplicationpublic class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public MethodValidationPostProcessor methodValidationPostProcessor() { return new MethodValidationPostProcessor(); }}希望对您有帮助。



