Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PathVariable in Spring Controller

Tags:

I'm trying to map the url /locations/{locationId}/edit.html - that seems to work with this code:

@Controller @RequestMapping( "/locations" ) public class LocationController {   @RequestMapping( value = "/{locationId}/edit.html", method = RequestMethod.GET )   public String showEditForm( Map<String, Object> map, @PathVariable int locationId )   {     map.put( "locationId", locationId );     return "locationform";   } } 

Call the mentioned url results in an exception:

java.lang.IllegalArgumentException: Name for argument type [int] not available, and parameter name information not found in class file either. 

Am I using the @PathVariable Annotation in a wrong way?

How to use it correctly?

like image 873
dtrunk Avatar asked Jan 31 '12 10:01

dtrunk


People also ask

What is PathVariable in Spring?

@PathVariable is a Spring annotation which indicates that a method parameter should be bound to a URI template variable. If the method parameter is Map<String, String> then the map is populated with all path variable names and values. It has the following optional elements: name - name of the path variable to bind to.

What is use of @PathVariable?

The @PathVariable annotation is used to extract the value from the URI. It is most suitable for the RESTful web service where the URL contains some value. Spring MVC allows us to use multiple @PathVariable annotations in the same method. A path variable is a critical part of creating rest resources.

What is difference between @PathParam and @PathVariable?

@PathParam: it is used to inject the value of named URI path parameters that were defined in @Path expression. @Pathvariable: This annotation is used to handle template variables in the request URI mapping ,and used them as method parameters.

What is @RequestParam and @PathVariable?

1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.


1 Answers

it should be @PathVariable("locationId") int locationId

like image 62
Moinul Hossain Avatar answered Oct 06 '22 13:10

Moinul Hossain