When using Spring Api RequestParam, is it possible to disallow "" or whitespace without additional check if code?
@GetMapping("/test")
public List<StatesInfoVO> getTestStates(@RequestParam(required = true) List<String>
states) {
//...
}
Could there be an error when this request is received?
/test?status="" or /test?status=" "
CodePudding user response:
The solution would be marking your controller with @Validated
annotation and adding validation annotations to the request param as shown below.
@RestController
@Validated
public class TestController {
@GetMapping("/test")
public List<StatesInfoVO> getTestStates(@RequestParam @NotEmpty List<@NotBlank String> states) {
//...
}
}
For reference: Validation with Spring Boot - the Complete Guide
CodePudding user response:
Depends on your entity and dB setup, if the field is NOT NULL, then this will throw an error Null pointer, and no need for (if check). But if the field can be NULL, then you need to either validate the field with your own code or use spring boot validation as described above. Anyway, it's better to validate the payload in the front-end and then send it to the back-end.