25

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?

3 Answers 3

40

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

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

3 Comments

this is detailed here, and happens when your code is compiled without debugging information (docs.spring.io/spring/docs/3.2.x/spring-framework-reference/…) : 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
note that just compiling using "Debug As" won't necessarily include debug info in the project. Check your settings, as detailed here, and basically check all the debug sounding checkboxes!
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.
16

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

 public String showEditForm(
       @PathVariable("locationId") int locationId,
       Map<String, Object> map) {
    // ...
 }

Comments

1

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.

Comments

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.