SSM项目配置swaggerUI

 1、首先在pom.xml中添加依赖

<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>
据网上说,尽量需要保证两者版本一致,不然会导致未知BUG。还有尽管版本一致,但是有时还会产生其他BUG,比如配置好之后,进swagger-ui.html会出现对话框且关不掉。


2、新建config包,建立SwaggerConfig.java文件,添加如下内容:
import com.wlhse.controller.APIInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

//如果你的项目引入junit测试,此处需要使用@WebAppConfiguration,如果没有使用junit使用@Configuration
@WebAppConfiguration
@EnableSwagger2//重要!
@EnableWebMvc
@ComponentScan(basePackages = "com.wlhse.controller")//扫描control所在的package请修改为你control所在package
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XX项目接口文档")
.description("XX项目接口测试")
.version("1.0.0")
.termsOfServiceUrl("")
.license("")
.licenseUrl("")
.build();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}

@Bean
APIInterceptor localInterceptor() {
return new APIInterceptor();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
super.addInterceptors(registry);
}
}
由于我自定义了拦截器,所以需要重写两个方法,如果没有拦截器,只需要Docket、ApiInfo。具体可以参考https://www.jianshu.com/p/a8cbfadf1289

3、在原有的SpringMVC.xml配置文件中添加如下内容:
<!--重要!将你的SwaggerConfig配置类注入-->
<bean class="com.wlhse.config.SwaggerConfig" />
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />

还需要在<mvc:interceptors>中添加:
<mvc:exclude-mapping path="/swagger*/**"></mvc:exclude-mapping>
<mvc:exclude-mapping path="/v2/**"></mvc:exclude-mapping>
<mvc:exclude-mapping path="/webjars/**"></mvc:exclude-mapping>


4、在controller上添加swaggerUI的注解,然后运行tomcat
打开http://localhost:8080/你的项目名称/swagger-ui.html,如果页面正常显示,说明我们已经成功在项目里添加swagger2支持。

如果出现bug,请检查你的版本,版本可能导致很多问题!!!
 







posted on 2020-03-12 18:26  coldpills  阅读(998)  评论(0编辑  收藏  举报