package com.sunreal.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* @author lkz
* @description Swagger3.0的注解改为@EnableOpenApi
* Swagger页面: http://localhost:8080/swagger-ui/index.htm
* pom.xml <dependency>
* <groupId>io.springfox</groupId>
* <artifactId>springfox-boot-starter</artifactId>
* <version>3.0.0</version>
* </dependency>
*
* Swagger2.x配置注解@EnableSwagger2
* pom.xml <dependency>
* <groupId>com.github.xiaoymin</groupId>
* <artifactId>swagger-bootstrap-ui</artifactId>
* <version>1.9.6</version>
* </dependency>
* <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>
* @date 2020/11/7 15:16
*/
@Getter
@Setter
@Configuration
@EnableOpenApi
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfig {
/**
* JSR303需要使用
* @ConfigurationProperties(prefix = "swagger")注解
* 并配置对象前缀,需要Set与Get方法
* SpringBoot的EL表达式可直接取值,搭配
* @PropertySource(value = "classpath:application.properties")
* @Value("${swagger.power}")
*/
private Boolean power;
/**
* 配置Swagger的Docket实例
* @return
*/
@Bean
public Docket docket(Environment environment) {
/**
* 除了yml配置开关,
* 还可以根据环境判断是否开启Swagger
* swagger:
* power: true
* spring:
* profiles:
* active: master
*/
Profiles profiles = Profiles.of("dev", "test");
boolean flag = environment.acceptsProfiles(profiles);
power = flag;
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
// 配置API文档分组
.groupName("测试")
// 是否开启Swagger
.enable(power)
.select()
// RequestHandlerSelectors 配置要扫描接口的方式
// .basePackage("com.**.**") 指定要扫描的包
// .any() 扫描全部
// .none() 都不扫描
// .withClassAnnotation(RestController.class) 扫描类上的注解(只扫描有这个注解的类)
// .withMethodAnnotation(GetMapping.class) 扫描有指定注解的方法
.apis(RequestHandlerSelectors.basePackage("com.sunreal.controller"))
// .paths() 过滤器
// PathSelectors.ant("/site/**") 只扫描指定前缀的请求
.paths(PathSelectors.ant("/api/v0/**"))
.build();
}
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API接口文档")
.description("内容描述")
.contact(new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱"))
.version("1.0")
.build();
}
}
@Data
@ApiModel(value = "用户实体类")
@AllArgsConstructor
public class User {
@ApiModelProperty(value = "用户名")
private String name;
@ApiModelProperty(value = "密码")
private String pwd;
}
@Api("测试查询接口")
@RequestMapping(value = "/api/v0")
@RestController
public class HelloController {
@ApiOperation(value = "人员查询", notes = "人员查询")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "path"),
@ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "String", paramType = "path")
})
@PostMapping(value = "/findUser//{name}/{pwd}")
public User user(@PathVariable String name, @PathVariable String pwd) {
return new User("测试", "成功");
}
}
1536

被折叠的 条评论
为什么被折叠?



