使用Swagger注释的意外令牌错误

时间:2020-04-08 13:11:40

标签: java grails swagger

我正在使用从Swagger核心Github复制的代码测试Swagger注释。

我正在测试的代码摘录来自here(第446行)的静态类SimpleOperations

在我的代码中,它看起来像这样:

(...) 

@Controller("/")
class IntegratorWebController {

    def convoyWebService

    @Operation(
            operationId = "subscribe",
            description = "subscribes a client to updates relevant to the requestor's account, as identified by the input token.  The supplied url will be used as the delivery address for response payloads",
            parameters = {
                @Parameter(in = ParameterIn.PATH, name = "subscriptionId", required = true,
                        schema = @Schema(implementation = Convoy.class),
                        style = ParameterStyle.SIMPLE, example = "example",
                        examples = {
                            @ExampleObject(name = "subscriptionId_1", value = "12345",
                                    summary = "Subscription number 12345", externalValue = "Subscription external value 1"),
                            @ExampleObject(name = "subscriptionId_2", value = "54321",
                                    summary = "Subscription number 54321", externalValue = "Subscription external value 2")
                        })
            },
            responses = {
                @ApiResponse(
                        description = "test description",
                        content = @Content(
                                mediaType = "*/*",
                                schema = @Schema(
                                        type = "string",
                                        format = "uuid",
                                        description = "the generated UUID",
                                        accessMode = Schema.AccessMode.READ_ONLY,
                                        example = "Schema example"
                                ),
                                examples = {
                                    @ExampleObject(name = "Default Response", value = "SubscriptionResponse",
                                            summary = "Subscription Response Example", externalValue = "Subscription Response value 1")
                                }
                        ))
            })

    def saveOrUpdateActivity(){
        def result = [error:[]]
        def status = OK

   (...)

唯一的区别是,我将ExamplesTest.SubscriptionResponse.class替换为代码中存在的类。

我正在使用

  • io.swagger.core.v3,swagger注释版本2.1.2
  • Java 11
  • Grails 4.0.2

我得到:

IntegratorWebController.groovy: 28: unexpected token: @ @ line 28, column 2.
        @Operation(
    ^

在IDE中,它看起来像这样:

enter image description here

Javadoc表示@Parameter可以在Operation或方法级别独立使用,以向操作添加参数,即使未绑定到任何方法参数也是如此。因此,例子就是声音。

怎么了?

谢谢!

1 个答案:

答案 0 :(得分:1)

因为我是用Groovy编写代码,所以@Parameters和@ExampleObject的数组必须使用[]而不是{}。

正确的代码如下:

    @Post(uri="/saveOrUpdateActivity", produces = MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Operation(
            operationId = "subscribe",
            description = "subscribes a client to updates relevant to the requestor's account, as identified by the input token.  The supplied url will be used as the delivery address for response payloads",
            parameters = [
                @Parameter(in = ParameterIn.PATH, name = "subscriptionId", required = true,
                        schema = @Schema(implementation = Convoy.class),
                        style = ParameterStyle.SIMPLE, example = "example",
                        examples = [
                            @ExampleObject(name = "subscriptionId_1", value = "12345",
                                    summary = "Subscription number 12345", externalValue = "Subscription external value 1"),
                            @ExampleObject(name = "subscriptionId_2", value = "54321",
                                    summary = "Subscription number 54321", externalValue = "Subscription external value 2")
                        ])
            ],
            responses = [
                @ApiResponse(
                        description = "test description",
                        content = @Content(
                                mediaType = "*/*",
                                schema = @Schema(
                                        type = "string",
                                        format = "uuid",
                                        description = "the generated UUID",
                                        accessMode = Schema.AccessMode.READ_ONLY,
                                        example = "Schema example"
                                ),
                                examples = [
                                    @ExampleObject(name = "Default Response", value = "SubscriptionResponse",
                                            summary = "Subscription Response Example", externalValue = "Subscription Response value 1")
                                ]
                        ))
            ])