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?
@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.
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.
@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.
1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.
it should be @PathVariable("locationId") int locationId
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With