15

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

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
WeGa
  • 801
  • 4
  • 10
  • 24

3 Answers3

12

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)
Sergio
  • 614
  • 5
  • 13
  • 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.ServletRequestBindingException","message":"Missing request header 'X-Custom-Header' for method parameter of type String","path":"/api/"}` – user3105453 Sep 21 '16 at 12:50
  • 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.UnsatisfiedServletRequestParameterException: Parameter conditions "id!=" not met for actual request parameters: id={}. – WeGa Oct 06 '16 at 07:04
  • WeGa how did u resolved it? Can you elaborate it please. – asifaftab87 Mar 26 '18 at 02:58
10

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

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • 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? – WeGa Sep 10 '15 at 08:58
-4

You can use defaultValue as in

@RequestParam(value = "id", required = true, defaultValue = "-1") String id

But, again you'll have to use an if like

if(id.equals("-1")){
   return "someErrorPage"
}

Otherwise you can create a filter for this purpose, as given in this example

How to use a servlet filter in Java to change an incoming servlet request url?

Community
  • 1
  • 1
Pallav Jha
  • 3,409
  • 3
  • 29
  • 52
  • Thanks for reply. But isn't my manual parameter check better than your solution? – WeGa Sep 10 '15 at 08:51
  • 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. – kryger Nov 22 '16 at 14:14