How to check request parameter is not empty String in @RequestMapping params?

36,174

Solution 1

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)

Solution 2

If you want to be Java EE compliant with validations use @Size(min=1).

Hibernate Validator has @NotEmpty annotation for this purpose. But that's not part of Java spec.

BTW, keep required=true as above notifications wouldn't enforce presence of the param in request.

EDIT reaction on comment:

Spring is Java EE Validation compliant, so @Size annotation should work if you have some Java EE validation API implmentor on class path. I used only hibernate validator so far. So you can enable these validation features by adding this into classpath:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.2.1.Final</version>
</dependency>

than you can annotate your controller with @Validated annotation and do this:

@RequestMapping(value = someURL, params = {"id"})
public SomeResponse doSomething(@RequestParam(required = true) @Size(min=1) String id)

You can also customize error message that will be sent to client.

If you are not familiar with Java EE validation, I would suggest to start looking into it as it's super important if you are creating REST/HTTP endpoint. It will open new world for you.

Start with Java EE Tutorial for validation

Relevant part of Spring doc

Share:
36,174
WeGa
Author by

WeGa

Updated on September 21, 2020

Comments

  • WeGa
    WeGa over 3 years

    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

  • WeGa
    WeGa over 8 years
    Thanks for reply. But isn't my manual parameter check better than your solution?
  • WeGa
    WeGa over 8 years
    Thanks for reply. I have update my post with environment description I have. I'm not sure it is compatible with the solution you have offered. Could you tell me, please, what would happened if input parameter violates these annotation constraints? How should I handle such situation in that case?
  • user3105453
    user3105453 over 7 years
    I noticed the 'required = true' isn't even necessary as I got a status 400 when I left out a header field: {"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.ServletRe‌​questBindingExceptio‌​n","message":"Missin‌​g request header 'X-Custom-Header' for method parameter of type String","path":"/api/"}
  • WeGa
    WeGa over 7 years
    Thanks, I think this is what I need. When I defined annotation the way you proposed, put breakpoint at the beginning of controller method and ran test, I a)didn't get into controller method; b)got self-explained exception org.springframework.web.bind.UnsatisfiedServletRequestParame‌​terException: Parameter conditions "id!=" not met for actual request parameters: id={}.
  • kryger
    kryger over 7 years
    Not only "not very helpful", but plain wrong. defaultValue is only used if the parameter is not specified, it doesn't cover the "was blank" scenario.
  • asifaftab87
    asifaftab87 about 6 years
    WeGa how did u resolved it? Can you elaborate it please.