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,請檢查你的版本,版本可能導致很多問題!!!
 








免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM