参考:前后端分离必备工具:Swagger快速搞定(整合SpringBoot详细教程)
1.导进swagger2需要的依赖
!--swagger配置依赖 用3.0 会报错-->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.编写HelloController测试
package com.codel.web.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@CrossOrigin //解除跨域问题
@GetMapping("/hello")
public String hello1() {
return "hello word!";
}
}
测试成功!
3.编写Swagger配置类
package com.codel.web.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 //开启swagger
public class SwaggerConfig {
//配置Swagger的Docket的bean实例 也就是存放接口的容器
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// .enable(false)//配置是否启动swagger,默认为true
/*
* apis():指定扫描的接口
* RequestHandlerSelectors:配置要扫描接口的方式
* basePackage:指定要扫描的包
* any:扫面全部
* none:不扫描
* withClassAnnotation:扫描类上的注解(参数是类上注解的class对象)
* withMethodAnnotation:扫描方法上的注解(参数是方法上的注解的class对象)
*/
.select().apis(RequestHandlerSelectors.basePackage("com.codel.web.controller"))
/*
* paths():过滤路径
* PathSelectors:配置过滤的路径
* any:过滤全部路径
* none:不过滤路径
* ant:过滤指定路径:按照按照Spring的AntPathMatcher提供的match方法进行匹配
* regex:过滤指定路径:按照String的matches方法进行匹配
*/
// .paths(PathSelectors.ant("/web/**"))
.paths(PathSelectors.any())
.build();
//配置Swagger信息
}
//配置Swagger页面的基本信息
private ApiInfo apiInfo() {
return new ApiInfo(
"codeL",
"我的Swagger API文档",
"1.0",
"https://bareth.blog.csdn.net/",
new Contact("codeL", "https://blog.csdn.net/Lesliesuai", "3189144168@qq.com"),//作者信息
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
}
配置Swagger配置类里面可能会用到是属性:
4.测试进入Sawgger页面
重启主程序,访问 localhost:8080/swagger-ui.html
5.配置Swagger API信息
就是上面的swagger是可以通过自定义来配置 的,配置一个docket容器,再创建一个apiInfo方法把修改的属性返回来,就可以通过修改apiInfo的属性来修改swagger的页面信息。
如果要修改swagger里面的接口信息,就要再docket里面配置docket的属性信息 ,去实现相对应的配置信息。
//配置Swagger的Docket的bean实例 也就是存放接口的容器
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// .enable(false)//配置是否启动swagger,默认为true
/*
* apis():指定扫描的接口
* RequestHandlerSelectors:配置要扫描接口的方式
* basePackage:指定要扫描的包
* any:扫面全部
* none:不扫描
* withClassAnnotation:扫描类上的注解(参数是类上注解的class对象)
* withMethodAnnotation:扫描方法上的注解(参数是方法上的注解的class对象)
*/
.select().apis(RequestHandlerSelectors.basePackage("com.codel.web.controller"))
/*
* paths():过滤路径
* PathSelectors:配置过滤的路径
* any:过滤全部路径
* none:不过滤路径
* ant:过滤指定路径:按照按照Spring的AntPathMatcher提供的match方法进行匹配
* regex:过滤指定路径:按照String的matches方法进行匹配
*/
// .paths(PathSelectors.ant("/web/**"))
.paths(PathSelectors.any())
.build();
//配置Swagger信息 其中.select().apis.paths.build是一套组合进行使用
}
//配置Swagger页面的基本信息
private ApiInfo apiInfo() {
return new ApiInfo(
"codeL",
"我的Swagger API文档",
"1.0",
"https://bareth.blog.csdn.net/",
new Contact("codeL", "https://blog.csdn.net/Lesliesuai", "3189144168@qq.com"),//作者信息
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
6.配置是否启动Swagger
在docket里面开启
.enable(false)//配置是否启动swagger,默认为true
配置不同环境下不同的权限:
开发环境:application-dev.properties
server.port=8081
正式环境:application-pro.properties
server.port=8082
然后在主配置文件application.properties中激活开发环境
spring.profiles.active=dev
然后我们到SwaggerConfig中的docket()方法中添加代码:
传进一个关于环境的参数Environment
//配置Swagger的Docket的bean实例 也就是存放接口的容器
@Bean
public Docket docket(Environment environment) {
// 开启适合的环境
//设置要配置的Swagger环境
Profiles profiles = Profiles.of("dev", "test");
//通过environment.acceptsProfiles判断是否处在自己设定的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)//配置是否启动swagger,默认为true
端口没有权限的
有权限的:
7.配置API文档分组
在docket通过.groupName中设置组名
// 配置组名
.groupName("codeL")
配置多个组
一个Docket实例就对应着一个组,因此配置多个docket就对应着多个组
@Bean
public Docket docket2() {
return new Docket(DocumentationType.SWAGGER_2).groupName("codehello");
}
配置完成后,就会由两个组
8.配置Model实体类
package com.codel.web.pojo;
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@GetMapping("/getUser")
public User getUser() {
return new User();
}
测试:
swagger出现了实体类。
9.常用注解
我们可以在实体类上和其属性上添加注解来添加对应的注释
@ApiModel //为类添加注释
@ApiModelProperty //为类属性添加注释
@ApiModel("用户类")
public class User {
@ApiModelProperty("用户名")
private String name;
@ApiModelProperty("密码")
private String password;
刷新后的界面:
注释类和方法
@Api(tags = "控制类") //注释类
@ApiOperation("得到用户的类") //注释方法
@RestController
//注释类
@Api(tags = "控制类")
public class Hello {
@CrossOrigin //解除跨域问题
@GetMapping("/hello")
public String hello1() {
return "hello word!";
}
@ApiOperation("得到用户的类") //注释方法
@GetMapping("/getUser")
public User getUser() {
return new User();
}
}
10.使用swagger
使用post就要在请求体里面传json值,实践方法一样。
测试error错误也是一样的方法步骤。