一:導入swagger相關依賴
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
二:編寫SwaggerConfig配置類
多文檔配置:
package com.springboot_swagger.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 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;
@Configuration
@EnableSwagger2 //開啓Swagger2
public class SwaggerConfig {
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("A"); //如何配置多個分組?
} //配置多個Docket實例
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("C");
}
//配置了swagger的docket的bean實例
@Bean
public Docket docket(Environment environment){
Profiles profiles = Profiles.of("dev","pro"); //設置要顯示的swagger環境
boolean flag = environment.acceptsProfiles(profiles); //environment.acceptsProfiles判斷是否處在自己設定的環境中
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag) //enable(flag),是否啓動swagger,如果爲false,則不能在瀏覽器中訪問swagger
.groupName("zym") //配置API文檔的分組
.select()
.apis(RequestHandlerSelectors.basePackage("com.springboot_swagger.controller")) //掃描指定包下的接口
.build();
}
//配置swagger信息-apiInfo
private ApiInfo apiInfo(){
//contact-作者信息
Contact contact = new Contact("zym","https://www.baidu.com","[email protected]");
return new ApiInfo("zym的swaggerAPI文檔",
"風華絕代",
"v1.0",
"https://www.baidu.com",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
三:編寫controller提供API接口和實體類
User.java
package com.springboot_swagger.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用戶實體類")
public class User {
@ApiModelProperty("用戶名")
public String username; //private屬性不會顯示在swagger中
@ApiModelProperty("密碼")
public String password;
}
HelloController.java
package com.springboot_swagger.controller;
import com.springboot_swagger.pojo.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello,swagger";
}
//只要接口返回值中,有實體類,就會被掃描到swagger中
@GetMapping("/user")
public User user(){
return new User();
}
//Operation接口,放在方法上
@ApiOperation("hello方法")
@GetMapping("/hello2")
public String hello2(String username){
return "hello" + username;
}
@PostMapping("/post")
public User post(User user){
return user;
}
}