1.springboot集成swagger依赖包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.配置swagger
package com.qjq.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {
}
3.查看接口地址
4.配置
package com.qjq.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.GetMapping;
import org.springframework.web.bind.annotation.RestController;
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;
@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {
@Bean
public Docket docket(Environment environment){
//配置了swagger的docket的bean实例
//如果配置多套环境用Profiles 因为可能生产环境不需要swagger
Profiles profiles=Profiles.of("dev","test");
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("狂神")//分组
.enable(flag)//enable 是否启动swagger false表示不启动,浏览器不能访问swagger页面,true代表启动
.select()
//RequestHandlerSelectors 配置要扫描的接口的方式
//any() 扫描全部
//none() 都不扫描
//basePackage("com.qjq.controller") 指定要扫描的包
//withClassAnnotation(RestController.class) 扫描带有这个注解的类
//withMethodAnnotation(GetMapping.class) 扫描带有这个注解的方法
.apis(RequestHandlerSelectors.basePackage("com.qjq.controller"))
//paths 过滤
//ant("/qjq/**")只扫描这个路径下面的接口
.paths(PathSelectors.ant("/qjq/**"))
.build();
}
//配置swagger信息 apiinfo
public ApiInfo apiInfo(){
//作者信息
Contact DEFAULT_CONTACT = new Contact("qjq", "", "");
return new ApiInfo("qjq的api文档",
"帅",
"1.0",
"urn:tos",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
5.配置多个分组,配置多个docket
//分组,不同的人写的不同的接口
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("C");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Bean
public Docket docket4(){
return new Docket(DocumentationType.SWAGGER_2).groupName("D");
}
6.配置扫描接口
package com.qjq.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
===========================
package com.qjq.controller;
import com.qjq.pojo.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
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";
}
//只要我们的接口中,返回值中存在实体类。他就会被扫描到swagger中
@ApiOperation("user接口")
@PostMapping("/user")
public User user(){
return new User();
}
}