如何创建分组的Swagger注释-此位置不允许使用注释

时间:2019-03-06 11:45:35

标签: java spring swagger swagger-ui swagger-2.0

我想对这个注释进行分组:

@ApiResponses(value= {
    @ApiResponse(
            code = 200, 
            message = "Successful status response"),
    @ApiResponse(
            code = 400, 
            message = "Successful status response"),
    @ApiResponse(
            code = 500, 
            message = "Successful status response"),

})

在单个注释@ResponseCodes中以将其缩短。 我发现this SO Question基本上有相同的问题。 此线程中的解决方案是创建一个自定义批注,在我的情况下如下所示:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ApiResponses(value= {
        @ApiResponse(
                code = 200, 
                message = "Successful status response"),
        @ApiResponse(
                code = 400, 
                message = "Successful status response"),
        @ApiResponse(
                code = 500, 
                message = "Successful status response"),
})

public @interface ResponseCodes {
}

但是,当我尝试此操作时,我收到@ApiResponses is disallowed in this location.

的错误消息

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我通过将依赖项版本更改为2.9.2来解决此问题,方法是:

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>

注释结果:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@ApiResponses(value = {
		@ApiResponse(code = 200, message = STATUS_CODE_200), @ApiResponse(code = 204, message = STATUS_CODE_204),
		@ApiResponse(code = 400, message = STATUS_CODE_400), @ApiResponse(code = 401, message = STATUS_CODE_401),
		@ApiResponse(code = 403, message = STATUS_CODE_403), @ApiResponse(code = 404, message = STATUS_CODE_404),
		@ApiResponse(code = 500, message = STATUS_CODE_500)
})
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface WBossResponses {
	
	

}