SpringBoot接口 - 如何生成接口文档之Swagger技术栈?( 三 )

其中ResponseStatus封装
package tech.pdai.springboot.knife4j.constant;import lombok.AllArgsConstructor;import lombok.Getter;import java.util.Arrays;import java.util.Collections;import java.util.List;/** * @author pdai */@Getter@AllArgsConstructorpublic enum ResponseStatus {SUCCESS("200", "success"),FAIL("500", "failed"),HTTP_STATUS_200("200", "ok"),HTTP_STATUS_400("400", "request error"),HTTP_STATUS_401("401", "no authentication"),HTTP_STATUS_403("403", "no authorities"),HTTP_STATUS_500("500", "server error");public static final List<ResponseStatus> HTTP_STATUS_ALL = Collections.unmodifiableList(Arrays.asList(HTTP_STATUS_200, HTTP_STATUS_400, HTTP_STATUS_401, HTTP_STATUS_403, HTTP_STATUS_500));/*** response code*/private final String responseCode;/*** description.*/private final String description;}Controller接口package tech.pdai.springboot.knife4j.controller;import io.swagger.annotations.Api;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import tech.pdai.springboot.knife4j.entity.param.AddressParam;/** * Address controller test demo. * * @author pdai */@Api(value = https://www.isolves.com/it/cxkf/kj/2022-07-14/"Address Interfaces", tags = "Address Interfaces")@RestController@RequestMapping("/address")public class AddressController {/*** http://localhost:8080/address/add .** @param addressParam param* @return address*/@ApiOperation("Add Address")@PostMapping("add")@ApiImplicitParams({@ApiImplicitParam(name = "city", type = "query", dataTypeClass = String.class, required = true),@ApiImplicitParam(name = "zipcode", type = "query", dataTypeClass = String.class, required = true)})public ResponseEntity add(AddressParam addressParam) {return ResponseEntity.ok("success");}}运行测试自定义用户主页

SpringBoot接口 - 如何生成接口文档之Swagger技术栈?

文章插图
 
model模型
SpringBoot接口 - 如何生成接口文档之Swagger技术栈?

文章插图
 
全局参数 和配置
SpringBoot接口 - 如何生成接口文档之Swagger技术栈?

文章插图
 
自定义文档
SpringBoot接口 - 如何生成接口文档之Swagger技术栈?

文章插图
 
接口文档和测试接口
SpringBoot接口 - 如何生成接口文档之Swagger技术栈?

文章插图
 
示例源码其它旧版本的实现:
  • swagger2
  • Swagger2+BootstrapUI
  • Knife4j v2
更多例子都可在如下仓库中找到
https://github.com/realpdai/tech-pdai-spring-demos
更多内容告别碎片化学习,无套路一站式体系化学习后端开发: Java 全栈知识体系(https://pdai.tech)

【SpringBoot接口 - 如何生成接口文档之Swagger技术栈?】


推荐阅读