SpringBoot整合Swagger
本文最后更新于2022.10.11-05:35
,某些文章具有时效性,若有错误或已失效,请在下方留言或联系涛哥。
作用
是一个动态生成接口文档的一个框架,用于和前端或测试岗位的同事进行对接。
实现案例
<!--导入swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--swagger用来生成前端页面-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2,新建一个config文件夹并新建以下文件
SwaggerConfig.java
package com.xxgc.helloworld.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.w3c.dom.DocumentType;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否开启前端ui显示 生产环境调为false
.enable(true)
.select()
//扫描有哪些接口(controller)要生成文档
.apis(RequestHandlerSelectors.basePackage("com.xxgc.helloworld.controller"))
//接口中所有路径都扫描
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
//api文档标题
.title("HelloWorld工程")
//文档描述
.description("工程文档描述")
//服务条款url
.termsOfServiceUrl("https://www.baidu.com")
//版本号
.version("1.0.0")
.build();
}
}
InterceptorConfig.java
package com.xxgc.helloworld.config;
import com.xxgc.helloworld.interceptor.JWTInterceptors;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
/**
* @program: helloword
* @description: 拦截器配置
* @author: liutao
* @create: 2022-03-07 16:11
**/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
//配置拦截器规则
public void addInterceptors(InterceptorRegistry registry){
//swagger相关
ArrayList<String> swagger = new ArrayList<>();
swagger.add("/*.html");
swagger.add("/swagger-resources/**");
swagger.add("/webjars/**");
swagger.add("/v2/**");
swagger.add("/swagger-ui.html/**");
}
}
3,使用
controller.java
package com.xxgc.helloworld.controller;
import com.xxgc.helloworld.po.Info;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@PropertySource(value = "classpath:system.properties")
@RequestMapping("/info")
@Api(value="hello接口",tags ="用于返回helloworld",description = "测试helloworld")
public class HelloController {
@Value("${system.name}")
private String system;
@ApiOperation(value = "获取helloworld",notes = "获取helloworld")
@GetMapping("/hello")
@ResponseBody
public Info hello(){
return new Info(200,system+"hello world");
}
@ApiOperation(value = "给什么返回什么",notes = "你给什么返回什么")
@GetMapping("/getMsg")
@ResponseBody
public Info getMsg(@ApiParam(name="msg",value="信息",required = true) String msg){
return new Info(-201,msg);
}
}
效果图
阅读剩余
版权声明:
作者:涛哥
链接:https://ltbk.net/back/spring_family/spring-boot/article/1223.html
文章版权归作者所有,未经允许请勿转载。
作者:涛哥
链接:https://ltbk.net/back/spring_family/spring-boot/article/1223.html
文章版权归作者所有,未经允许请勿转载。
THE END