24

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?

dtrunk
  • 4,685
  • 17
  • 65
  • 109

3 Answers3

40

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

Moinul Hossain
  • 2,176
  • 1
  • 24
  • 30
  • 8
    this is detailed here, and happens when your code is compiled without debugging information (http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html) : **if the URI template variable name matches the method argument name you can omit that detail. As long as your code is not compiled without debugging information, Spring MVC will match the method argument name to the URI template variable name** – TheWestIsThe... Nov 20 '13 at 19:16
  • note that just compiling using "Debug As" won't necessarily include debug info in the project. Check your settings, [as detailed here](http://stackoverflow.com/a/1318483/1412656), and basically check all the debug sounding checkboxes! – TheWestIsThe... Nov 20 '13 at 19:27
  • Just want to add that there seemingly are other reasons. I just upgraded from Spring Boot 1 to 2, same JDK version, and this problem suddenly appeared in a test that also was having a problem with an AOP aspect, so I'm wondering if that's related. – Sander Verhagen May 06 '19 at 04:15
16

You should add the value argument to your @PathVariable, e.g.,

 public String showEditForm(
       @PathVariable("locationId") int locationId,
       Map<String, Object> map) {
    // ...
 }
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
0

JDK 7 enables parametername introspection

Parametername exposition is available in JDK7, otherwise you must set it in the Annotation.

You should use the JDK exposition before explicitly use it (like Johan and Moniul suggested) as part of the annotation because if you like to change the parameter-key you need to edit only the variable-name and not any other occourences in annotation-specifications in other lines and/or classes. Lets call it single-source-of-truth.

Grim
  • 1,938
  • 10
  • 56
  • 123