当前位置: 首页 > news >正文

swagger 2.10.5 整合 spring boot

参考:
http://springfox.github.io/springfox/
https://github.com/springfox/springfox
http://springfox.github.io/springfox/docs/current/
https://github.com/springfox/springfox-demos
https://github.com/springfox/springfox-demos/tree/2.9.2
https://github.com/springfox/springfox-demos/tree/3.0.01.引入依赖<!-- (io.springfox):springfox-swagger2, springfox-swagger-ui --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.10.5</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.10.5</version></dependency><!-- 2.10.5 版本需要该依赖:springfox-spring-webmvc --><dependency><groupId>io.springfox</groupId><artifactId>springfox-spring-webmvc</artifactId><version>2.10.5</version></dependency>2.配置类import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;/*** 访问:http://localhost:8080/swagger-ui.html#/*/
@Configuration
@EnableSwagger2WebMvc
public class Swagger2Config {
    @Value("${spring.profiles.active}")private String active;private String basePackage = "com.xxx.xxx.controller";@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).enable(!"prod".equals(active)).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("Redis API 接口文档").description("Redis REST API 接口文档").termsOfServiceUrl("").contact(new Contact("维护人xxx","维护人url","维护人email")).license("xxx License Version 2.0").licenseUrl("http://www.xxx.xxx/licenses/LICENSE-2.0").version("1.0").build();}}3.Controller层与入参实体类编写:实体类:
@ApiModel
@NoArgsConstructor
@AllArgsConstructor
@Data
public class MapVo {@ApiModelProperty(value = "key")private String key;@ApiModelProperty(value = "value")private String value;
}处理控制器:
@Api(value = "xxx接口", tags = "xxx 接口操作")
@Controller
@RequestMapping("/string")
public class TestConroller {@Autowiredprivate RedisTemplate<String, String> redisTemplate;@ApiOperation("set 操作 - 实体类")@ResponseBody@RequestMapping(value = "/setvo", method = RequestMethod.POST)public String setValues(@RequestBody MapVo mapVo) {redisTemplate.opsForValue().set(mapVo.getKey(), mapVo.getValue());return "success";}@ApiOperation("get 操作")@ResponseBody@RequestMapping(value = "/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)public String getValue(String key) {String result = redisTemplate.opsForValue().get(key);return result;}@ApiOperation("set 操作")@ResponseBody@RequestMapping(value = "/set", method = RequestMethod.GET)public String setValue(String key, String value) {redisTemplate.opsForValue().set(key, value);return "success";}@ApiOperation("set value 操作")@ResponseBody@RequestMapping(value = "/set", method = RequestMethod.POST)public String setVal(String key, String value) {redisTemplate.opsForValue().set(key, value);return "success set val";}}4.访问:http://localhost:8080/swagger-ui.html#/5.常用注解@EnableSwagger2WebMvc
@Api
@ApiOperation
@ApiModel 和 @ApiModelProperty
@ApiParam
@ApiImplicitParams 和 @ApiImplicitParam
@ApiResponses 和 @ApiResponse
http://www.lryc.cn/news/144600.html

相关文章:

  • Python 练习:剔除数字
  • Linux系统编程:基础知识入门学习笔记汇总
  • 基于硬件隔离增强risc-v调试安全1_问题描述
  • OpenCV简介
  • Windows下编译qt-src-5.15.10
  • 有关linux排查服务器资源问题
  • 【设计模式】Head First 设计模式——观察者模式 C++实现
  • 【ES】笔记-Promise基本使用
  • 服务器数据恢复-reiserfs文件系统损坏如何恢复数据?
  • 直播预告:把脉2023年下半场—主动防御邮箱盗号威胁
  • 专题:平面、空间直线参数方程下的切线斜率问题
  • JavaScript—对象与构造方法
  • 微信小程序社区户口管理的系统设计与实现
  • 闲人闲谈PS之四十六——网络生产全流程
  • 如何在VR头显端实现低延迟的RTSP或RTMP播放
  • 【工具类】提高办公效率(兼具有趣、好玩)
  • navicat连接数据库的方法(易懂)
  • 收支明细管理实操:如何准确记录并修改收支明细?
  • SSL证书的工作原理是怎样的?
  • Java发送请求到第三方(RestTemplate方法)
  • CentOS 7 Nacos 设置开机自动重启
  • 安防监控平台EasyCVR视频汇聚平台增加首页告警类型的详细介绍
  • 构建安全可信、稳定可靠的RISC-V安全体系
  • 3.RabbitMQ 架构以及 通信方式
  • 分布式事务篇-2.1 阿里云轻量服务器--Docker--部署Seata
  • C语言这么没用??
  • Docker运维篇
  • 【数学建模】清风数模正课7 多元线性回归模型
  • 文心一言 VS 讯飞星火 VS chatgpt (83)-- 算法导论8.1 4题
  • 温故知新之:代理模式,静态代理和动态代理(JDK动态代理)