문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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

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

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top