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

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

3 Comments

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/"}
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 how did u resolved it? Can you elaborate it please.
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

1 Comment

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?
-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?

2 Comments

Thanks for reply. But isn't my manual parameter check better than your solution?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.