jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.7</version>
</dependency>
<!--springfox的核心jar包 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--springfox-ui的jar包(里面包含了swagger的界面静态文件) -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger增强页面,默认的太丑 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.9</version>
</dependency>
package com.mhm.listener;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
@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/");
registry.addResourceHandler("/doc.html").addResourceLocations(
"/doc.html");
}
}
加入swagger2的配置类
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;
import springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration;
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = "org.mohrss.leaf.demo.****")
@Import({Swagger2DocumentationConfiguration.class})
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
// 扫描全部
// .apis(RequestHandlerSelectors.any())
// 扫描Api修饰的类
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
// 扫描ApiOperation修饰的方法
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XXX项目接口文档")
.description("XXX项目接口测试")
.version("1.0.0")
.termsOfServiceUrl("")
.license("")
.licenseUrl("http://localhost:8080/sitp_portal/swagger-ui.html")
.build();
}
}
package com.mhm.swagger;
import io.swagger.models.Path;
import io.swagger.models.Swagger;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
@Aspect
@EnableAspectJAutoProxy
@Component
public class SwaggerApiSuffixAspect {
@AfterReturning(pointcut="execution(public io.swagger.models.Swagger springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl.mapDocumentation(..))",
returning="swagger")
public void doBeforeBussinessCheck(Swagger swagger){
Map<String, Path> paths = swagger.getPaths();
if(null != paths){
Map<String, Path> newPaths = new HashMap<String, Path>(paths);
paths.clear();
Iterator<String> it = newPaths.keySet().iterator();
while(it.hasNext()){
String oldKey = it.next();
// 添加模式后缀 .do
String newKey = oldKey + ".do";
paths.put(newKey, newPaths.get(oldKey));
}
newPaths = null;
}
}
}
spring-mvc.xml配置文件中加入swagger2的支持
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**"
location="classpath:/META-INF/resources/webjars/" />
<bean id="/swaggerConfig" class="com.mhm.swagger.SwaggerConfig" />
web.xml文件中添加对swagger的支持
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!-- 这个是对struts2的支持 -->
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
<!-- 这个是对swagger2的支持 -->
<url-pattern>/v2/api-docs</url-pattern>
<url-pattern>/swagger-resources</url-pattern>
<url-pattern>/swagger-resources/configuration/security</url-pattern>
<url-pattern>/swagger-resources/configuration/ui</url-pattern>
</servlet-mapping>
controller写法
@Api(tags = "测试控制器")
public class TestController {
@ApiOperation("列表查询")
@RequestMapping("getPageList")
@ResponseBody
public PageResult<XXX> findList(PageCondition pageCondition) {
return service.findList(pageCondition);
}
}