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/"/>