springMVC 4.3.1.RELEASE + swagger2 2.8.0 + JSON 2.8.7

本文介绍如何将SpringMVC 4.3.1.RELEASE与Swagger2 2.8.0及JSON 2.8.7进行整合。强调了JSON版本的重要性,指出2.4版本会有报错,推荐使用2.8.7。配置过程简单,界面美观。文章提供了pom.xml中相关依赖的版本信息,创建Swagger2配置类的步骤,以及如何添加文档内容和访问接口文档的URL。

此处为应用版本工具包都较高,特别注意JSON版本号,之前尝试过2.4版本的,会报错,直接改为2.8.7版本的,其他版本是否兼容,并未尝试,可自行尝试

本文较之前版本,配置简单,页面也更加美观

一、pom.xml文件 添加依赖

最重要的三个是spring、json、swagger2的版本号,spring的依赖此处不再赘述,以下贴上json和swagger2的依赖版本号:

<!-- Jackson Json处理工具包 -->
<jackson.version>2.8.7</jackson.version>
       <dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
	    <version>${jackson.version}</version>
	</dependency>

	<dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-core</artifactId>  
            <version>${jackson.version}</version>
        </dependency>  
         
        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-annotations</artifactId>  
            <version>${jackson.version}</version> 
        </dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
	    <artifactId>jackson-module-jaxb-annotations</artifactId>
             <version>${jackson.version}</version>
        </dependency>
<!-- 集成swagger 要配置json处理工具,上文已配置过 -->
        <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>

二、创建Swagger2配置类:

@Configuration 
@EnableWebMvc 
@EnableSwagger2 
public class SwaggerConfig { 
	@Bean 
	public Docket createRestApi() { 
		return new Docket(DocumentationType.SWAGGER_2) 
				.apiInfo(apiInfo()) 
				.select() 
				.apis(RequestHandlerSelectors.basePackage("com.weixin.controller.ice")) 
				.paths(PathSelectors.any()) .build(); 
		} 
	private ApiInfo apiInfo() { 
		return new ApiInfoBuilder() 
				.title("APP RESTful APIs") 
				.description("在线文档:http://www.xxx.xxx") 
				.termsOfServiceUrl("http://www.xxxx.com.cn/") 
				.contact(new Contact("yueyue", "www.baidu.com", "1875410193@qq.com")) 
				.version("0.5.1") 
				.build(); 
		} 
}

三、添加文档内容

通过注解反应在接口文档上

 访问路径:http://localhost:8080/deer/swagger-ui.html#/

deer为项目命名,请自行修改

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值