Swagger 创建带有虚张声势注释的公共零部件
我正在为我的spring boot项目使用swagger注释 我想为控制器的每个资源返回一个公共响应代码契约Swagger 创建带有虚张声势注释的公共零部件,swagger,swagger-2.0,Swagger,Swagger 2.0,我正在为我的spring boot项目使用swagger注释 我想为控制器的每个资源返回一个公共响应代码契约 @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @ApiResponses(value = { @ApiResponse(code = 400, message = "Bad stuff from the client"), @ApiResponse(code = 404
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Bad stuff from the client"),
@ApiResponse(code = 404, message = "Item not found") }
)
public @interface GlobalApiReponses {
}
在文件中:
他们谈论@ApiResponses,但我不能将注释放在类级别
以下是我所做的:
@Api(value = "Title",
description = "What this controller is about"
)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Bad stuff from the client"),
@ApiResponse(code = 404, message = "Item not found") }
)
public class FooBarController {
...
}
但问题是,400-来自客户端的坏东西
和404-未找到的项目
从未显示在生成的文档中
在《大摇大摆》的官方文件中,我看到了这一部分:
问题:如何创建一种带有java注释的“可重用组件”
谢谢根据文档,您可以在摘要级别执行此操作
.useDefaultResponseMessages(false)
.globalResponseMessage(RequestMethod.GET,
newArrayList(new ResponseMessageBuilder()
.code(400)
.message("Bad stuff from the client")
.build()))
更新:
如果要执行注释管线,可以创建自己的管线并将其放置在控制器上
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Bad stuff from the client"),
@ApiResponse(code = 404, message = "Item not found") }
)
public @interface GlobalApiReponses {
}
那就用它吧
@Api(value = "Title",
description = "What this controller is about"
)
@GlobalApiReponses
public class FooBarController
多种方法的结合可能也是一个不错的选择
@Target(ElementType.TYPE)意味着您可以在类级别应用它。您可以使用ElemenType.METHOD对方法执行相同的操作。这正是我想要的。这是有效的。谢谢,没问题。很高兴我能提供帮助。@Hermannstidel关于如何为没有类型目标的招摇过市注释创建自定义注释的提示?例如
ApiModelProperty