筆者最近拿springmvc4.x+swagger 2.8(最新版本)集成過(guò)程中碰到一些問(wèn)題,現(xiàn)在分享一下
1谭确、引入的包
swagger2 的包 maven倉(cāng)庫(kù)上找的最新版
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
2巴元、如果啟動(dòng)有報(bào)jackjson的錯(cuò)者蠕,請(qǐng)?jiān)僖雑ackjson的包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.6</version>
</dependency>
3檩奠、我看過(guò)很多版本的集成翁锡,有些是不引入swagger-ui包蔓挖,那么你就需要去自己下載swagger-ui 然后把 dist目錄下的文件全部拷貝到自己的靜態(tài)資源下。
我們需要把swagger的訪問(wèn)配置下靜態(tài)路徑
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
4馆衔、項(xiàng)目里配置swagger
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;
/**
* @Author zhangwenfeng
* @Date 2018/1/17
* @Description
*/
@EnableWebMvc
@EnableSwagger2
@Configuration
public class RestApiConfig extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.zx.user.controller.api"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("用戶中心api")
.termsOfServiceUrl("www.baidu.com")
.contact("xxxx")
.version("1.1")
.build();
}
}
5瘟判、最后一個(gè)注意點(diǎn),如果使用的是 fastjson 請(qǐng)把版本升級(jí)到1.2.15以上角溃。
6拷获、使用 swagger-ui包的 訪問(wèn)路徑為 http://{ip}:{port}:{project}/swagger-ui.html
7、在被掃描的controller里 加swagger注解减细,具體注解使用方式自行百度.