"絲襪哥"Swagger來(lái)了(整合SpringBoot)
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
作者 | Baret-H
來(lái)源 | urlify.cn/3qq26j
學(xué)習(xí)目標(biāo):
一、Swagger簡(jiǎn)介
1. 前后端分離
2. Swagger引入
二、SpringBoot集成Swagger
1. 新建springboot項(xiàng)目
2. 導(dǎo)入Swagger依賴
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
3. 編寫HelloController測(cè)試
package com.zsr.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
4. 編寫Swagger配置類
package com.zsr.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2//開啟Swagger2
public class SwaggerConfig {
}
5. 測(cè)試進(jìn)入Sawgger頁(yè)面
<!-- 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>
6. 配置Swagger API信息
package com.zsr.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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//開啟Swagger2
public class SwaggerConfig {
//配置Swagger的Docket的bean實(shí)例
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());//配置Swagger信息
}
//配置Swagger信息
private ApiInfo apiInfo() {
return new ApiInfo(
"Baret-H",
"我的Swagger API文檔",
"1.0",
"https://bareth.blog.csdn.net/",
new Contact("Baret-H", "https://bareth.blog.csdn.net/", "[email protected]"),//作者信息
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
}
7. 配置Swagger自定義掃描接口
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//配置Swagger信息
.select()
/**
* apis():指定掃描的接口
* RequestHandlerSelectors:配置要掃描接口的方式
* basePackage:指定要掃描的包
* any:掃面全部
* none:不掃描
* withClassAnnotation:掃描類上的注解(參數(shù)是類上注解的class對(duì)象)
* withMethodAnnotation:掃描方法上的注解(參數(shù)是方法上的注解的class對(duì)象)
*/
.apis(RequestHandlerSelectors.basePackage("com.zsr.controller"))
/**
* paths():過(guò)濾路徑
* PathSelectors:配置過(guò)濾的路徑
* any:過(guò)濾全部路徑
* none:不過(guò)濾路徑
* ant:過(guò)濾指定路徑:按照按照Spring的AntPathMatcher提供的match方法進(jìn)行匹配
* regex:過(guò)濾指定路徑:按照String的matches方法進(jìn)行匹配
*/
.paths(PathSelectors.ant("/zsr/**"))
.build();
}
.paths(PathSelectors.any)
.apis(RequestHandlerSelectors.basePackage("com.zsr.controller"))
8. 配置是否啟動(dòng)Swagger
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//配置Swagger信息
.enable(false)//配置是否啟動(dòng)swagger,默認(rèn)為true
.select()
.apis(RequestHandlerSelectors.basePackage("com.zsr.controller"))
.paths(PathSelectors.ant("/zsr/**"))
.build();
}
spring.profiles.active=dev
Environment environment
@Configuration
@EnableSwagger2//開啟Swagger2
public class SwaggerConfig {
//配置Swagger的Docket的bean實(shí)例
@Bean
public Docket docket(Environment environment) {
//設(shè)置要配置的Swagger環(huán)境
Profiles profiles = Profiles.of("dev", "test");
//通過(guò)environment.acceptsProfiles判斷是否處在自己設(shè)定的環(huán)境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//配置Swagger信息
.enable(flag)//通過(guò)flag判斷是否開啟
.select()
.apis(RequestHandlerSelectors.basePackage("com.zsr.controller"))
.paths(PathSelectors.ant("/zsr/**"))
.build();
}
//配置Swagger信息
private ApiInfo apiInfo() {
return new ApiInfo(
"Baret-H",
"我的Swagger API文檔",
"1.0",
"https://bareth.blog.csdn.net/",
new Contact("Baret-H", "https://bareth.blog.csdn.net/", "[email protected]"),//作者信息
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
}
spring.profiles.active=pro
9. 配置API文檔分組
1. 設(shè)置默認(rèn)組名
public Docket docket(Environment environment) {
//設(shè)置要配置的Swagger環(huán)境
Profiles profiles = Profiles.of("dev", "test");
//通過(guò)environment.acceptsProfiles判斷是否處在自己設(shè)定的環(huán)境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//配置Swagger信息
.groupName("zsr")
.enable(true)//配置是否啟動(dòng)swagger,默認(rèn)為true
.select()
.apis(RequestHandlerSelectors.basePackage("com.zsr.controller"))
.paths(PathSelectors.any())
.build();
}
2. 配置多個(gè)組
@Bean
public Docket docket1() {
return new Docket(DocumentationType.SWAGGER_2).groupName("Baret-H");
}
@Bean
public Docket docket2() {
return new Docket(DocumentationType.SWAGGER_2).groupName("鐘");
}
10. 配置Model實(shí)體類
1. 新建實(shí)體類
package com.zsr.pojo;
public class User {
public String username;
public String password;
}
2. 編寫對(duì)應(yīng)請(qǐng)求接口
@GetMapping("/getUser")
public User getUser() {
return new User();
}
3. 啟動(dòng)測(cè)試
4. 常用注解
package com.zsr.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用戶實(shí)體")
public class User {
@ApiModelProperty("用戶名")
public String username;
@ApiModelProperty("密碼")
public String password;
}
@Api(tags = "Hello控制類")
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
@ApiOperation("hello控制類")
@GetMapping("/getUser")
public User getUser() {
return new User();
}
}
11. 測(cè)試Swagger的使用
1. 測(cè)試傳參
@GetMapping("/username")
public String getUserName(@ApiParam("用戶名") String username) {
return username;
}
@PostMapping("/username")
public String getUserName(@ApiParam("用戶名") String username) {
return username;
}
2. 測(cè)試錯(cuò)誤
package com.zsr.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用戶實(shí)體")
public class User {
@ApiModelProperty("用戶名")
public String username;
@ApiModelProperty("密碼")
public String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@PostMapping("/post")
private User postUser(User user) {
int i = 100 / 0;
return user;
}


評(píng)論
圖片
表情
































