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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
Hmm okay. Could you try changing the signature to public int findAll(@PathVariable Optional<Integer> no, HttpServletRequest request) { and seeing what Spring thinks the URL is in debug mode? (request.getRequestURL())
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.

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.