一:导入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;
}
}