package com.zjh.swagger.config;
import com.google.common.base.Predicates;
import com.zjh.swagger.annotion.Swagger4SAnnotion;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@Configuration
public class SwaggerConfig {
/**
* docket是Swagger的全局配置bean
* 他是默认有参构造来选择生成文档类型
*
* docket.apiInfo 是为配置中添加api帮助信息
* ApiInfo apiInfo=new ApiInfoBuilder()
* apiinfo默认是构建器模式生成
*后可以自己设置一些属性然后用build生成完全信息类
* contact属性是对文档生成者的一个描述,需要new 一个Concat
*
* docket.select()//获取swagger配置类中的选择器 返回apiSelectorBuilder 人话说就是给他配置显示规则器
* .apis(Predicates.not(//对布尔值进行取反
* RequestHandlerSelectors.withMethodAnnotation(Swagger4SAnnotion.class)//选择器通过方法上的注解进行获取
* ))//获取后取反 人话就是有这个注解的方法不被扫描生成文档
* .apis(RequestHandlerSelectors.basePackage("com.zjh.swagger.controller"))//这是对基础包的配置
* .build();//因为是获取了选择器配置了规则所以要重新构建一下才能成功配置
*
* // .paths( Predicates.or(//对下面进行配置
* // PathSelectors.regex("/.*"),
* // PathSelectors.regex("/swagger/.*")
* // )
* // )//配置了这个只会对在regex正则表达式下符合条件的路径生成swagger文档
*
* @return
*/
@Bean
public Docket docket(){
Docket docket=new Docket(DocumentationType.SWAGGER_2);
ApiInfo apiInfo=new ApiInfoBuilder().contact(
new Contact(
"支**",
"https://space.bilibili.com/9120650?spm_id_from=333.788.0.0",
"1156574080"))
.description("文档描述")
.title("api标题")
.version("2.0")
.build();
docket.apiInfo(apiInfo);
docket.select()//获取swagger配置类中的选择器 返回apiSelectorBuilder 人话说就是给他配置显示规则器
.apis(Predicates.not(//对布尔值进行取反
RequestHandlerSelectors.withMethodAnnotation(Swagger4SAnnotion.class)//选择器通过方法上的注解进行获取
))//获取后取反 人话就是有这个注解的方法不被扫描生成文档
.apis(RequestHandlerSelectors.basePackage("com.zjh.swagger.controller"))//这是对基础包的配置
// .paths( Predicates.or(//对下面进行配置
// PathSelectors.regex("/.*"),
// PathSelectors.regex("/swagger/.*")
// )
// )//配置了这个只会对在regex正则表达式下符合条件的路径生成swagger文档
.build();//因为是获取了选择器配置了规则所以要重新构建一下才能成功配置
return docket;
}
}
controller
package com.zjh.swagger.controller;
import com.zjh.swagger.annotion.Swagger4SAnnotion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
@Controller
@RestController
@Api(tags = {"myController的别名可以为数组"},description = "mycontroller的描述")
//对接口生成文档的一个别名和描述
public class MyController {
@GetMapping("/get")
public String getMapping(){
return "get";
}
@PostMapping("/post")
@ApiOperation(value = "post方法的描述",notes = "post方法的提示")
public String postMapping(@ApiParam(name="用户名",value ="用户名是字符串类型的描述",required = true) String name,@ApiParam(name="密码",value ="密码描述",required = true) String ps){
return "post";
}
@Swagger4SAnnotion
@RequestMapping("/req")
public String reqMapping(){
return "req";
}
@ApiIgnore//忽略不生成文档
@GetMapping("/get/{c}/{b}")
public String getMapping(@PathVariable String c, @PathVariable Integer b){
return "get";
}
}
//自定义注解
package com.zjh.swagger.annotion;
import org.springframework.stereotype.Component;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//自定义注解
@Component
@Target({ElementType.METHOD})//注解目标是一个数组,现在只放了一个在方法上可以使用
@Retention(RetentionPolicy.RUNTIME)//在什么时候存在 运行时
public @interface Swagger4SAnnotion {
String value() default "";
}
pojo类
package com.zjh.swagger.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
@ApiModel(value="自定义pojo类",description = "在作为值返回时才会解析生成文档")
public class Users implements Serializable {
@ApiModelProperty(value="主",name="name(姓名)",required = true,example = "案例",hidden = false)
private String name;
public Users() {
}
public Users(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//启动时需要开启swagger和做一点配置
@SpringBootApplication
@EnableSwagger2//开启swagger功能
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
yaml配置,不配置报错
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher