文章目录
引入pom
两个依赖
<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>
配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//扫描com.wzw的所有controller
.apis(RequestHandlerSelectors.basePackage("com.wzw"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("哈哈哈哈哈哈哈哈title")
.description("哈哈哈哈哈哈哈哈description")
.termsOfServiceUrl("哈哈哈哈哈哈哈哈termsOfServiceUrl")
.contact("哈哈哈哈哈哈哈哈contact")
.version("哈哈哈哈哈哈哈哈version")
.build();
}
}
controller
@RestController
@Api(tags = "测试接口")
@RequestMapping("/test")
public class TestController {
@ApiOperation(value="hello的value",notes="hello的notes")
@RequestMapping("/hello")
@ApiImplicitParams({
@ApiImplicitParam(name="id的name",value="id的value"),
@ApiImplicitParam(name="name的name",value="name的value")
})
public String hello(String id,String name){
return "hello";
}
@RequestMapping("/aaa")
public String aaa(){
return "hello";
}
}
访问swagger界面
http://localhost:8080/swagger-ui.html
swagger常用注解
这些注解相当于给swagger界面的接口参数的注释
@Api
@Api
用在controller上
@RestController
@Api(tags = "测试接口")
@RequestMapping("/test")
public class TestController {
效果:
@ApiOperation
@ApiOperation
用在方法上
@ApiOperation(value="hello的value",notes="hello的notes")
@RequestMapping("/hello")
public String hello(String id,String name){
return "hello";
}
效果:
@ApiImplicitParam
@ApiImplicitParam
用在参数上
@ApiOperation(value="hello的value",notes="hello的notes")
@RequestMapping("/hello")
@ApiImplicitParams({
@ApiImplicitParam(name="id的name",value="id的value"),
@ApiImplicitParam(name="name的name",value="name的value")
})
public String hello(String id,String name){
return "hello";
}
效果:
报错
Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
springboot版本和swagger2版本不符。
- 降低springboot版本,
- 或者配置文件增加参数
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher