Springboot整合swagger的步骤以及出现问题的解决
第一步:引入swagger的依赖文件:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency> 这里的版本为2.7.0。
第二步:写配置文件,纯Java配置。
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxx.demo.web"))//扫描的是自己写Controller的包名。
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")//swagger的title
.description("简单优雅的restfun风格")//自己定义
.termsOfServiceUrl("http://blog.csdn.net/")//自己定义
.version("1.0")
.build();
}
这样的话,当自己启动Springboot启动类,然后按照当初自己给的端口(如果自己在配置文件里面配置了访问的时候必须加项目名称,哪自己的记住!)例如我访问的就是:http://localhost:8080/swagger-ui.html。这样就可以看到自己定义的接口了。但如果自己要写测试类测试自己写的接口,按照这种方式配置的swagger,在进行单元测试的时候会报错误。如下:
org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$cfbcb6cb] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-05-16 11:58:07.671 WARN 11268 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered