Spring Boot groovy 编译错误:Unexpected token @@line 45 [英] Spring Boot groovy compilation error: Unexpected token @ @ line 45

查看:34
本文介绍了Spring Boot groovy 编译错误:Unexpected token @@line 45的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Spring Boot 项目中的 Java 控制器转换为 Groovy,并在尝试编译和运行时遇到最奇怪的错误

I'm trying to convert a Java controller in my Spring Boot project to Groovy, and getting the strangest error when trying to compile and run

unexpected token: @ @ line 45, column 5
@RequestMapping(value = {"/v1/foo", "/foo"}, method = GET)
^

这让我很困惑.注释是 Java 或 Groovy 中的注释,对吗?我错过了什么?这是我的代码的抽象

This is baffling to me. Annotations are annotations in Java or Groovy, right? what am I missing? Here's an abstraction of my code

// 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 之间的一个关键区别

So I'm just dumb and missed a key differentiator between Java and Groovy

问题是我传递给 @RequestMapping

在 Java 中,{"/v1/foo", "foo"} 是一个数组字面量

In Java, {"/v1/foo", "foo"} is an array literal

在 Groovy 中,{"/v1/foo", "foo"} 是一个闭包

In Groovy, {"/v1/foo", "foo"} is a closure

该错误消息显然没有帮助,但要解决此问题,我只需要更改 Groovy 中的注释以按照我的意图传入数组文字,而不是闭包

The error message obviously wasn't helpful, but to fix this I simply needed to change the annotation in Groovy to pass in an array literal as I intended, not a closure

@RequestMapping(value = ["/v1/foo", "/foo"], method = GET)

这篇关于Spring Boot groovy 编译错误:Unexpected token @@line 45的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆