'springboot @validated and bindingresult

@Controller
@RequestMapping("/")
@Validated
public class AddressWebController {
    @GetMapping(value = {"/{id}")
    public String editForm(@PathVariable(value = "id") @Positive Long id, Model model) {
            // process
    }

    @PutMapping(value = {"/edit"})
    public String editProcess(@ModelAttribute @Valid Form form,
                              BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            // ???
        }
        
        // process
    }
}

public class Form {
    @NotBlank
    private String name;
}
  • curl -XGET 'http://localhost/5'

    • is pass
  • curl -XGET 'http://localhost/-5'

    • A ConstraintViolationException exception is thrown. Due to @Validated and @Positive
  • curl 'http://localhost/edit' --data-raw '_method=PUT&name='

    • I expected it to come in as bindingResult.hasErrors() in that part, but the same ConstraintViolationException is thrown.

When @Validated is removed, bindingResult.hasErrors() works, but @Positive annotation does not. How can I use @Validated annotation and BindingResult together?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source