In my controller I have String parameter, containing some id, that should not be null of empty string. I'm wondering, is there any way to check it is not empty String in @RequestMapping params? I have tried to solve it in some ways
@RequestMapping(value = someURL, params = {"id"})
public SomeResponse doSomething(@RequestParam(required = true) String id)
@RequestMapping(value = someURL, params = {"!id="})
public SomeResponse doSomething(@RequestParam(required = true) String id)
@RequestMapping(value = someURL, params = {"!id=\"\""})
public SomeResponse doSomething(@RequestParam(required = true) String id)
with no success.
As I understand, both params = {"id"}
and @RequestParam(required = true)
can only check that parameter id
is presented in request (!= null).
It is most likely that I have to check that with code in controller boby, like
if (id == null || id.isEmpty()) {
return someErrorResponse;
}
but please correct me if I wrong. Thanks in advance.
P.S. my app is running on Java 1.7 SE in Apache Tomcat 7.0.62 container
Method parameters annotated with @RequestParam are required by default. will correctly invoke the method. When the parameter isn't specified, the method parameter is bound to null.
2) @RequestParam is more useful on a traditional web application where data is mostly passed in the query parameters while @PathVariable is more suitable for RESTful web services where URL contains values.
In Spring MVC, the @RequestParam annotation is used to read the form data and bind it automatically to the parameter present in the provided method. So, it ignores the requirement of HttpServletRequest object to read the provided data.
@RequestParam makes Spring to map request parameters from the GET/POST request to your method argument. @RequestBody makes Spring to map entire request to a model class and from there you can retrieve or set values from its getter and setter methods.
According to the Spring code that consumes that annotation
org.springframework.web.servlet.mvc.annotation.ServletAnnotationMappingUtils.checkParameters(String[], HttpServletRequest)
Something like this should work:
@RequestMapping(value = someURL, params = {"id!="})
public SomeResponse doSomething(@RequestParam(required = true) String id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With