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