0

I have requirement to pass a Optional parameter (count) to GET method. I tried below.

@RequestMapping(value = {"/findDetail","/findDetail/{no}"}, method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
     public int findAll(@PathVariable Optional<Integer>  no) {
        //find method takes Optional argument.
        return ticketService.find(no);
    }

I am expecting some value here but no always has null. Did I miss something here?

Jacob
  • 420
  • 5
  • 15

1 Answers1

0

I just tried this out, and it worked fine for me? no.get() gives 4 with a GET to /findDetail/4

I'm running with Spring MVC 4.2.6 (via Spring Boot 1.3.5) if that makes any difference?

Do you see a change if you do @PathVariable("no") Optional<Integer> no instead? (I can't see why you would, but worth a shot?)

But you definitely are on the right 'path' - what you said should work fine as pointed at in Optional path segments in Spring MVC amongst other answers.

Community
  • 1
  • 1
Tristan Perry
  • 607
  • 6
  • 14
  • I tried with same Spring version as yours. I have deployed it in tomcat. While running in debug mode to check the value, I am not at all the value – Jacob May 24 '16 at 16:47
  • Hmm okay. Could you try changing the signature to `public int findAll(@PathVariable Optional no, HttpServletRequest request) {` and seeing what Spring thinks the URL is in debug mode? (`request.getRequestURL()`) – Tristan Perry May 25 '16 at 08:55
  • Actually when I give multiple value in Request mapping, it doesnt work. The call even doesnt come to method. But when I give single value like value = "/findDetail/ or value = "/findDetail/{no}, it works as expected for that url. – Jacob May 25 '16 at 14:25