Swagger Api 开发文档

常用到的注解有:

  • Api
  • ApiModel
  • ApiModelProperty
  • ApiOperation
  • ApiParam
  • ApiResponse
  • ApiResponses
  • ResponseHeader

1. api标记

Api 用在类上,说明该类的作用。可以标记一个Controller类做为swagger 文档资源,使用方式:

@Api(value = "/user", description = "Operations about user")

与Controller注解并列使用。 属性配置:

属性名称备注
valueurl的路径值
tags如果设置这个值、value的值会被覆盖
description对api资源的描述
basePath基本路径可以不配置
position如果配置多个Api 想改变显示的顺序位置
producesFor example, "application/json, application/xml"
consumesFor example, "application/json, application/xml"
protocolsPossible values: http, https, ws, wss.
authorizations高级特性认证时配置
hidden配置为true 将在文档中隐藏

在SpringMvc中的配置如下:

 
  1. @Controller

  2. @RequestMapping(value = "/api/pet", produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})

  3. @Api(value = "/pet", description = "Operations about pets")

  4. public class PetController {

  5.  
  6. }

2. ApiOperation标记

ApiOperation:用在方法上,说明方法的作用,每一个url资源的定义,使用方式:

 
  1. @ApiOperation(

  2. value = "Find purchase order by ID",

  3. notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",

  4. response = Order,

  5. tags = {"Pet Store"})

与Controller中的方法并列使用。
属性配置:

属性名称备注
valueurl的路径值
tags如果设置这个值、value的值会被覆盖
description对api资源的描述
basePath基本路径可以不配置
position如果配置多个Api 想改变显示的顺序位置
producesFor example, "application/json, application/xml"
consumesFor example, "application/json, application/xml"
protocolsPossible values: http, https, ws, wss.
authorizations高级特性认证时配置
hidden配置为true 将在文档中隐藏
response返回的对象
responseContainer这些对象是有效的 "List", "Set" or "Map".,其他无效
httpMethod"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS" and "PATCH"
codehttp的状态码 默认 200
extensions扩展属性

在SpringMvc中的配置如下:

 
  1. @RequestMapping(value = "/order/{orderId}", method = GET)

  2. @ApiOperation(

  3. value = "Find purchase order by ID",

  4. notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",

  5. response = Order.class,

  6. tags = { "Pet Store" })

  7. public ResponseEntity<Order> getOrderById(@PathVariable("orderId") String orderId)

  8. throws NotFoundException {

  9. Order order = storeData.get(Long.valueOf(orderId));

  10. if (null != order) {

  11. return ok(order);

  12. } else {

  13. throw new NotFoundException(404, "Order not found");

  14. }

  15. }

3. ApiParam标记

ApiParam请求属性,使用方式:

public ResponseEntity<User> createUser(@RequestBody @ApiParam(value = "Created user object", required = true)  User user)

与Controller中的方法并列使用。

属性配置:

属性名称备注
name属性名称
value属性值
defaultValue默认属性值
allowableValues可以不配置
required是否属性必填
access不过多描述
allowMultiple默认为false
hidden隐藏该属性
example举例子

在SpringMvc中的配置如下:

 
  1. public ResponseEntity<Order> getOrderById(

  2. @ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true)

  3. @PathVariable("orderId") String orderId)

4. ApiResponse

ApiResponse:响应配置,使用方式:

@ApiResponse(code = 400, message = "Invalid user supplied")

与Controller中的方法并列使用。 属性配置:

属性名称备注
codehttp的状态码
message描述
response默认响应类 Void
reference参考ApiOperation中配置
responseHeaders参考 ResponseHeader 属性配置说明
responseContainer参考ApiOperation中配置

在SpringMvc中的配置如下:

 
  1. @RequestMapping(value = "/order", method = POST)

  2. @ApiOperation(value = "Place an order for a pet", response = Order.class)

  3. @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })

  4. public ResponseEntity<String> placeOrder(

  5. @ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {

  6. storeData.add(order);

  7. return ok("");

  8. }

5. ApiResponses

ApiResponses:响应集配置,使用方式:

 @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })

与Controller中的方法并列使用。 属性配置:

属性名称备注
value多个ApiResponse配置

在SpringMvc中的配置如下:

 
  1. @RequestMapping(value = "/order", method = POST)

  2. @ApiOperation(value = "Place an order for a pet", response = Order.class)

  3. @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })

  4. public ResponseEntity<String> placeOrder(

  5. @ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {

  6. storeData.add(order);

  7. return ok("");

  8. }

6. ResponseHeader

响应头设置,使用方法

@ResponseHeader(name="head1",description="response head conf")

与Controller中的方法并列使用。 属性配置:

属性名称备注
name响应头名称
description头描述
response默认响应类 Void
responseContainer参考ApiOperation中配置

在SpringMvc中的配置如下:

@ApiModel(description = "群组")

7. 其他

  • @ApiImplicitParams:用在方法上包含一组参数说明;
  • @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
    • paramType:参数放在哪个地方
    • name:参数代表的含义
    • value:参数名称
    • dataType: 参数类型,有String/int,无用
    • required : 是否必要
    • defaultValue:参数的默认值
  • @ApiResponses:用于表示一组响应;
  • @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息;
    • code: 响应码(int型),可自定义
    • message:状态码对应的响应信息
  • @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候;
  • @ApiModelProperty:描述一个model的属性。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要关闭 Swagger 开发文档,你可以在 Swagger 配置文件中将 enabled 属性设置为 false。例如,在 Spring Boot 中,你可以在 application.properties 文件中添加如下内容: ``` # Turn off Swagger springfox.documentation.swagger.v2.enabled=false ``` 然后重启应用程序即可生效。 此外,你也可以通过编程方式在运行时动态关闭 Swagger。例如,在 Spring Boot 中,你可以使用以下方式关闭 Swagger: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build() .enable(false); } } ``` 这样,当你需要启用 Swagger 时,只需将 enable 方法的参数设置为 true 即可。 ### 回答2: 关闭Swagger开发文档可以通过以下几种方式: 1. 删除Swagger依赖:在项目的构建文件中,移除Swagger相关的依赖。如果使用Maven构建项目,你可以在pom.xml文件中删除Swagger的依赖配置,并重新编译项目。如果使用其他构建工具,也要删除相关配置并重新构建项目。 2. 禁用Swagger配置:如果你想保留Swagger依赖,但不希望在生产环境中使用Swagger开发文档,你可以在项目的配置文件中禁用Swagger。具体的配置方式取决于你所使用的框架和工具,一般来说,可以通过设置配置项或注释来禁用Swagger相关的功能。这样,在生产环境中Swagger的UI和接口文档将不可用。 3. 限制Swagger访问权限:如果你想在部分环境中使用Swagger开发文档,可以通过限制Swagger的访问权限来达到关闭的效果。一种常见的做法是在应用的Web服务器(如Nginx)或网络代理层面配置访问控制规则,限制只有特定IP地址或特定用户可以访问Swagger的UI和接口文档。 4. 使用Swagger注解控制文档生成:Swagger会根据代码中的注解生成接口文档。如果你只想关闭文档生成,但保留Swagger依赖和UI,可以在代码中不使用Swagger的注解。将`@Api`、`@ApiOperation`等相关注解从你的接口和方法中移除,这样Swagger将不再解析和生成接口文档。 综上所述,关闭Swagger开发文档可以通过删除Swagger依赖、禁用配置、限制访问权限和不使用Swagger注解等方式实现。选择适合你项目需求和环境的方式,以达到你所期望的效果。 ### 回答3: 要关闭Swagger开发文档,可以采取以下几个步骤。 第一步,定位到项目中的Swagger配置文件。Swagger通常使用yaml或者json格式的配置文件来定义API文档。可以在项目文件中搜索swagger.yaml或者swagger.json等相关文件。 第二步,打开Swagger配置文件。使用文本编辑器打开找到的Swagger配置文件。 第三步,寻找文档配置项。在Swagger配置文件中,一般会有一个文档相关的配置项,比如"documentation"或者"info"。这个配置项包含了文档的基本信息和显示形式。 第四步,禁用Swagger文档。将文档配置项中的"enabled"或者"enable"的属性值设置为false,即禁用文档功能。如果没有这个属性,可以尝试将文档配置项删除或者注释掉。 第五步,保存并关闭Swagger配置文件。在编辑器中保存对Swagger配置文件的修改,并关闭编辑器。 第六步,重新编译或者重启项目。一些项目在Swagger配置文件修改后需要重新编译,或者重启应用程序,才能生效关闭Swagger开发文档。 完成以上步骤后,Swagger开发文档应该已经被关闭。可以通过访问项目的API地址来验证Swagger文档是否已关闭。如果访问API返回的是实际接口数据而不是Swagger文档,则说明已成功关闭Swagger开发文档

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值