我正在尝试将Spring Boot项目中的Java控制器转换为Groovy,并在尝试编译和运行时遇到最奇怪的错误
unexpected token: @ @ line 45, column 5 @RequestMapping(value = {"/v1/foo", "/foo"}, method = GET) ^
这让我感到困惑。注释是Java或Groovy中的注释,对吗?我想念什么?这是我的代码的抽象
// src/main/groovy/my/package/FooController.groovy, formerly .java /// ... proper imports @RestController @EnableAutoConfiguration public class FooController { // ... @autowire services @RequestMapping(value = {"/v1/foo", "/foo"}, method = GET) public ResponseEntity get(@RequestHeader HttpHeaders headers) { // do work return ResponseEntity } @RequestMapping(value = {"/v1/foo", "/foo"}, method = PUT) public ResponseEntity put(@RequestHeader HttpHeaders headers, @ResponseBody @Valid final MyFoo myFoo) { // do work return ResponseEntity } }
所以我只是愚蠢的,错过了Java和Groovy之间的关键区别
问题是value我要传递给@RequestMapping
value
@RequestMapping
在Java中{"/v1/foo", "foo"}是数组文字
{"/v1/foo", "foo"}
在Groovy中,{"/v1/foo", "foo"}是一个闭包
错误消息显然没有帮助,但是要解决此问题,我只需要更改Groovy中的注释即可按我的意图传递数组文字,而不是使用闭包
@RequestMapping(value = ["/v1/foo", "/foo"], method = GET)