pom
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>
配置文件
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
配置类中
// 配置类上标注
@EnableSwagger2WebMvc
@Bean
public Docket dockerBean() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("invoicing")
.description("# invoicing 接口文档")
.version("1.0")
.contact(new Contact("hhx","person_website","xxxx@qq.com"))
.build())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.guet.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
如果配置的有SpringSecurity,进行放行资源
.antMatchers("/doc.html",
"/v2/api-docs-ext/**",
"/favicon.ico",
"/webjars/**",
"/img.icons/**",
"/swagger-resources/**",
"/v2/api-docs/**",
"/swagger-ui/**"
).permitAll()
一些注解的作用
@Api:用于标注Controller,主要是对该Controller下的接口,进行一个总体的描述,如xxxx模块。
@ApiOperation:用在Controller方法上,说明该接口的作用
@ApiImplicitParam:用在@ApiImplicitParams注解中,也可以单独使用,说明一个请求参数的是否必传,参数含义,类型等等
@ApiParam:写在方法参数前面。用于对参数进行描述或说明是否为必添项等说明。
swagger常用注解
wagger信息配置与常用注解
@RequestParam的required参数为true的含义是:接口参数名一定要存在,但参数后面有没有值它不管