SSM项目使用springfox集成swagger

ssm项目的搭建可以参考博客http://blog.csdn.net/gebitan505/article/details/44455235/

已经写得非常详细了。

看了很多博客发现jar包的引用没有说清楚,其实还是有很多依赖jar包需要引入的。

ssm和springfox整合

首先上一张springfox的jar包依赖图


根据上图pom.xml的springfox的jar包依赖为

<!-- springfox swagger start-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>

<!-- springfox swagger end-->


其中jackon的jar包主要用于swagger的数据处理。

2.将swagger的文件拷贝到项目WEB-INF 下面

http://download.csdn.net/detail/s_discus/9715009

3.更改

index.html中的地址


按照这个格式 url = "http://localhost:8080/项目名称/v2/api-docs";

4.需要在common通用包里面添加Swagger2Config类 ,类名可以自己根据习惯修改。

import static springfox.documentation.builders.PathSelectors.regex;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;


import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import com.google.common.collect.Sets;


/**
 * Swagger描述
 */
@Configuration
@EnableWebMvc
@EnableSwagger2
public class Swagger2Config {


@Bean
public Docket configSpringfoxDocketForAll() {
return new Docket(DocumentationType.SWAGGER_2)
.produces(Sets.newHashSet("application/json"))
.consumes(Sets.newHashSet("application/json"))
.protocols(Sets.newHashSet("http")).forCodeGeneration(true)
.select().paths(regex(".*")).build().apiInfo(apiInfo());
}


private ApiInfo apiInfo() {
Contact contact = new Contact("公司标题", "公司网址",
"公司邮箱");
ApiInfo apiInfo = new ApiInfo("项目名 API REST API文档",
"使用Swagger UI构建CNPCMMS REST风格的可视化文档", "1.0.0",
"此处改为index中修改的HTML地址",contact,
"Apache License",
"http://www.apache.org/licenses/LICENSE-2.0.html");
return apiInfo;
}


}


5.这个类需要手动注入

 <bean class="文件路径.Swagger2Config" />

这样子利用springfox就把swagger集成到项目中了。

启动项目,输入地址http://localhost:8080/项目名/docApi/index.html

这样就可以了。

效果图就是看到swaggerui和在Swagger2Config中添加的抬头。

注意:index中的外部文件引用地址进行了重命名

外部文件重命名

<mvc:resources mapping="/swagger-ui/**" location="WEB-INF/docApi/" />

访问地址重命名
    <mvc:resources mapping="index.html" location="/WEB-INF/docApi/"/>

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值