When using validation for parameters of a Spring MVC @RequestMapping
method, Spring responds with with different status codes depending on the type of the parameter:
- For invalid
@RequestBody
parameters, Spring responds with 400 - For invalid
@RequestHeader
,@PathVariable
, and@RequestParam
parameters, Spring responds with 500.
Can this be changed so that Spring responds with the same 400 response in all cases?
This is my code:
@Controller
@Validated
public class WebController {
@RequestMapping(method = RequestMethod.POST, path = "/action")
public ResponseEntity<String> doAction(
@RequestHeader("Header-Name") @Valid LatinString headerValue,
@RequestBody @Valid Struct body) {
return new ResponseEntity<>(HttpStatus.OK);
}
}
public class LatinString {
@Pattern(regexp = "[A-Za-z]*")
private String value;
public LatinString(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
public class Struct {
@Pattern(regexp = "[A-Za-z0-9.-]{1,255}")
private String domain;
public String getDomain() {
return domain;
}
}