- Spring JPAチュートリアル に取り組んでいたときのこと
- Spring Boot 3.2.3を使用
MainController.java
...
@PostMapping(path="/add") // Map ONLY POST Requests
public @ResponseBody String addNewUser (@RequestParam String name, @RequestParam String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
...
- 以下のエラーが発生
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalArgumentException: 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.] with root cause
- RequestParamの変数名と対応する引数名が同じときはこの記法で大丈夫なはず、と思ったが、Spring Framework 6.1以降では
LocalVariableTableParameterNameDiscoverer
が使えなくなり、これに伴って省略記法も使えない(Spring Boot 3.2.3 はSpring Framework 6.1.4を使用)(参考) - https://github.com/spring-projects/spring-boot/issues/39095