- 环境 spring-web 4.0.2.RELEASE,JDK 1.8
2.引入JAR
<!--springfox的核心jar包-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<!--springfox-ui的jar包(里面包含了swagger的界面静态文件)-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
3.Spring-mvc.xml 文件,配置注入拦截的信息
拦截器不是整合Swagger 的,是其他地方使用到,贴出来只是提醒需要设置避开拦截的路径;
<context:annotation-config />
<!--重要!将你的SwaggerConfig配置类注入-->
<bean class="com.jhssms.config.SwaggerConfig"/>
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
4.Controller 注解
// 控制器
@Api(value = "/AdminController", tags = "Admin接口")
//方法
@ApiOperation(value = "查询用户信息", notes = "查询用户信息", httpMethod = "GET", response = PageResult.class)
5.访问路径
访问地址:http://localhost:7070/swagger-ui.html
6.效果图
注意: 1.多考虑是否有访问被拦截 ;
2.springfox-swagger2 和 springfox-swagger-ui 版本尽量保持一致;
其他:版本问题
java.lang.NoSuchMethodError: org.springframework.web.util.UriComponentsBuilder.fromHttpRequest(Lorg/springframework/http/HttpRequest;)Lorg/springframework/web/util/UriComponentsBuilder;
at springfox.documentation.swagger2.web.HostNamePr
原因:原因是spring-web版本不配对swagger2,UriComponentsBuilder内部方法找不到
解决办法:io.springfox俩个包若是2.7.0的版本,spring-web包的版本至少要4.1.6。搭配使用,要么spring整体升级版本,要么swagger降版本。