0

I want to fetch url parameter in my class. my application based on Spring MVC.

While calling url: http://localhost:8080/mywebapp/dir/register.do?id=26 it gives error 400

@RequestMapping(value = "/register")
public ModelAndView finalPage(@PathVariable("id") Long id) throws NumberFormatException, Exception {
   // code...
}

Anybody can solve my problem.

3
  • How do you think @PathVariable works and why? Commented Nov 8, 2014 at 17:57
  • Use @QueryParam instead of @PathVariable Commented Nov 8, 2014 at 18:00
  • @SotiriosDelimanolis I am not sure, is there any other way? Commented Nov 8, 2014 at 18:01

1 Answer 1

2

You say

I want to fetch url parameter in my class

but you are using @PathVariable which

indicates that a method parameter should be bound to a URI template variable

You don't have a uri template variable called id in your mapping.

What you really need is a @RequestParam. The documentation explains how to use it.

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

2 Comments

Thanks @SotiriosDelimanolis, I used "@RequestParam" and its work perfect. but what if i did not pass id in url? in some cases i am not passing parameter with url
@Bharat If you look at the javadoc I linked, you can set required to false.

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.