1

I would like to validate input of following @RequestMapping:

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public Response getCategory(@PathVariable("id") Long id) {
   // some logic here
}

When consumer of the endpoint passes string following error occurs:

Failed to convert value of type [java.lang.String] to required type [java.lang.Long]; nested exception is java.lang.NumberFormatException: For input string: "null"

I could change it to string but I believe there is a better way to do it.

pixel
  • 24,905
  • 36
  • 149
  • 251

1 Answers1

3

The answer from RC is a very good way to make sure your id will be made of digits.

In general if you want to validate incoming requests you could also create and register a custom interceptor by implementing HandlerInterceptor and then add your validation in the overridden preHandle method.

martidis
  • 2,897
  • 1
  • 11
  • 13