1. swagger分組(只需要創建幾個Docket類型的方法即可)
package com.Google.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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.EnableSwagger2;
import java.util.ArrayList;
import static springfox.documentation.spi.DocumentationType.SWAGGER_2;
@Configuration //將這個類交給springBoot托管
@EnableSwagger2 //啟用Swagger2
@EnableWebMvc
public class SwaggerConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
WebMvcConfigurer.super.addResourceHandlers(registry);
}
//配置多組信息(一個Docket為一個組)
@Bean
public Docket docket1(){
return new Docket(SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2(){
return new Docket(SWAGGER_2).groupName("B");
}
@Bean
public Docket docket3(){
return new Docket(SWAGGER_2).groupName("C");
}
//配置Swagger信息
@Bean
public Docket docket(Environment environment){
//設置要顯示的swagger環境
Profiles profils = Profiles.of("dev");
//通過 environment.acceptsProfiles判斷自己是否處於自己設定的環境
boolean flag = environment.acceptsProfiles(profils);
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.groupName("小落")
.enable(flag)//關閉swagger 這里我們可以根據生產環境,選擇開啟或者關閉swagger
.select()
//RequestHandlerSelectors:配置要掃描的包
//(一般就掃描basePackage)basePackage:指定要掃描的包
//any():全掃描
//none():全部掃描
//withMethodAnnotation():掃描方法上的注解 參數是注解的反射
.apis(RequestHandlerSelectors.basePackage("com.Google.controller"))
//過濾的路徑(不掃描)這里的路徑是指包的位置,而不是url
//ant:過濾指定的路徑
//any():過濾所有路徑
//none():不過濾
//regex():正則表達
.paths(PathSelectors.ant("/Google/*"))
.build();
}
private ApiInfo apiInfo(){
Contact contact = new Contact("小落", "", "2034281742@qq.com"); //作者信息(Contact 聯絡)
return new ApiInfo("小落的API", //文檔的標頭
"這個API用來測試", //文檔的描述
"1.0", //文檔的版本號
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}
注意需要@bean,交給springBoot接管
2.swagger注釋
用於實體類
@ApiModel("用戶實體類")
用於實體類參數
@ApiModelProperty("用戶名")
package com.Google.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("用戶實體類")
public class user {
@ApiModelProperty("用戶名")
String username;
@ApiModelProperty("密碼")
String password;
}
用於接口方法
@ApiOperation("userpost控制") //只放在接口上有用
用於接口參數
@ApiParam("用戶名")
package com.Google.controller;
import com.Google.pojo.user;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class helloSwagger {
@GetMapping("/hello")
public String hello(){
return "你好,Swagger";
}
//只要我們的接口中,返回值是實體類,它就會被Swagger掃描
@PostMapping("/postuser")
@ApiOperation("userpost控制") //只放在接口上有用
public user postuser(@ApiParam("用戶實體類參數") user user){
return user;
}
@GetMapping("/hello1")
public String hello1(@ApiParam("用戶名") String username){
return "你好,Swagger"+username;
}
}
小結;
- 在項目上線時Swagger要注意關閉,節省運行內存
