1

We're building a simple rest API using Spring Web MVC. One of our handlers takes as a request parameter an int called ID.

@RequestMapping(value="/requestEntity",
    method=RequestMethod.GET)
public ModelMap getEntity(
    @PathVariable(value="id")final Integer id,
    HttpServletResponse response) throws IOException  {

I know that the request object has the request parameter values as strings (name1=value1&name2=22) and that the handler will magically receive an int when it has ID as its parameter.

"/requestEntity", id="5" -> getEntity(5);

Unfortunately, if we send the value "1 2 3" as input, we get as an argument the number 123. If we send "5;123" we get 5 as an argument.

"/requestEntity", id="1 2 3" -> getEntity(123); // WRONG -- we want to throw an error
"/requestEntity", id="5;123" -> getEntity(5);   // ALSO WRONG

As far as I can tell, Spring is doing some conversion to turn the string parameter values into the int argument that the handler needs. How can we implement our own argument converter?

1 Answer 1

0

This looks like a similar problem to this question: Spring @MVC and @RequestParam validation. There are two answers/solutions that may work for you, I'd try this one first:

@RequestMapping
public void handleRequest( HttpServletRequest request ) {
    int page = ServletRequestUtils.getIntParameter(request, "page", 1);
}
Sign up to request clarification or add additional context in comments.

Comments

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.