spring boot+ swagger
依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
启用注解
@EnableSwagger2
配置
@Configuration
public class SwaggerConfig {
@Bean
public Docket swaggerSpringMvcPlugin() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
}
}
Controller注解
注意要加 RequestBody 或 RequestParam等注解
@ApiOperation(value = "test", httpMethod = "POST")
@PostMapping("t6")
public void t6(@RequestBody User user) {
}
配合@value注解可以控制在各种profile下是否开启,注意enable方法
@Bean
public Docket swaggerSpringMvcPlugin() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(new ApiInfo("","","","","","",""))
.enable(true);
}
地址
http://localhost:8081/swagger-ui.html
json生成地址
http://localhost:8081/v2/api-docs
json可视化地址