3

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)
^

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
    }
}

1 Answer 1

6

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

The problem is the value I'm passing to @RequestMapping

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

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

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.