Name for argument type [java.lang.String] ってどういうこと? (Spring Boot)
以下のような html から、コントローラにGET送信すると、エラーが出た。
empList.html
<td>
<form th:action="@{/employee/{id}(id=${emp.id})}" method="GET">
<input type="submit" value="編集">
</form>
</td>
EmployeeController.java
@GetMapping("/{id}")
public String showUpdate(EmployeeForm employeeForm, @PathVariable String id, Model model) {
System.out.println("id:" + id);
// (... 省略 ... )
return "entry";
}
エラー
Name for argument of type [java.lang.String] not specified,
and parameter name information not available via reflection.
Ensure that the compiler uses the ‘-parameters’ flag.
[java.lang.String] 型の引数の名前が指定されておらず、リフレク
ション経由でパラメータ名の情報を取得できません。
コンパイラが「-parameters」フラグを使用していることを確認してください。
結論
問題のコードでいうと、
public String showUpdate(EmployeeForm employeeForm, @PathVariable String id, Model model) {
のところ、
@PathVariable String id
と書くところ、次のようにも書けるということらしい。
@PathVariable("id") String id
これで動いた。
参考
このページでは、以下のような回答が書かれている。
It seems the issue is for this handler
@RequestMapping(method = RequestMethod.GET, value = "/{id}")
public ResponseEntity viewOrder(@PathVariable String id) {
since it is the only one that has a String parameter type.
The problem is that you have compiled the code in a way that doesn’t include parameter names. You can either compile with the parameter names, or explicitly provide a @PathVariable value like so
google訳
(問題は、パラメーター名を含まない方法でコードをコンパイルしたことです。 パラメーター名を使用してコンパイルすることも、次のように @PathVariable 値を明示的に指定することもできます。)
public ResponseEntity viewOrder(@PathVariable("id") String id) {
また、別の回答者からは、Eclipseのプロジェクトの設定を以下のようにすればよい
というのもあった。
プロジェクト名 --- プロパティ --- Javaコンパイラ ---
メソッド・パラメーターの情報を保管(S)(リフレクション経由で使用可能)
にチェックを入れる。
この方法でもいけた。
しかし、別のプロジェクトでは、ここにチェックを入れなくても
@PathVariable String id
で問題なく動いているんだが・・・。
カテゴリー: Java, memo, Spring
タグ: @PathVariable, name for argument type
カウント: 459