您的位置:首页 > 编程语言 > Java开发

springmvc集成Swagger自动生成api文档

2017-01-11 10:56 639 查看

springmvc集成Swagger自动生成api文档

准备工作

下载swaggerUI: https://github.com/swagger-api/swagger-ui

maven项目中,添加pom依赖

<dependencies>
...
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jcenter-release</id>
<name>jcenter</name>
<url>http://oss.jfrog.org/artifactory/oss-release-local/</url>
</repository>
</repositories>


配置swaggerconfig

/**
*@Copyright:Copyright (c) 2016 - 2018
*@Company:htkj
*/
package com.ht.controller.apidoc;

import org.springframework.beans.factory.annotation.Autowired;
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.WebMvcConfigurerAdapter;

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;

/**
* @Title:
* @Description:
* @Author:奋斗的大侠
* @Since:2017年1月10日
* @Version:1.1.0
*/
@Configuration
@EnableWebMvc
@EnableSwagger
@ComponentScan("com.ht.controller")
public class SwaggerConfig  extends WebMvcConfigurerAdapter{
private SpringSwaggerConfig springSwaggerConfig;
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
this.springSwaggerConfig = springSwaggerConfig;
}
@Bean
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(".*?");
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo(
"日志服务api",
"日志服务主要为解决,我们现有的系统和第三方交互时产生输入输出做记录,为后续运维第一时间确定问题的原因\r联系人:奋斗的大侠",
"日志服务目前接入端有 phoneClient,.netClient,javaClient",
"duanjj@htmitech.com",
"FREE",//Licence Type
"http://localhost:8080/logcrab/license");
return apiInfo;
}
}


spring配置文件的配置

<!-- 实例化swaggerconfig 建议放在mvc:annotation-driven 之后 -->
<bean class="com.ht.controller.apidoc.SwaggerConfig"/>
<!-- 静态资源(js/image)的访问 -->
<mvc:resources location="/" mapping="/**.html"/>
<mvc:default-servlet-handler/>


配置swagger页面

解压开始下载的swagger-ui的包,如图:



复制dist目录下所有文件到你工程指定的文件夹下,我指定的是docapi



访问

我的访问地址为:http://localhost:8080/log-crab/docapi:如图:



由于需要客户端来查看接口文档,通过我的ip来访问时,页面报错:



Can’t read from server. It may not have the appropriate access-control-origin settings.

解决办法: 在spring-mvc中加入跨域访问权限设置即可,也可以通过tomcat容器界别设置:

<mvc:cors>
<mvc:mapping path="/**"
allowed-origins="*"
allowed-methods="*"
allowed-headers="*"
allow-credentials="false" />
<mvc:mapping path="/docapi/**"/>
</mvc:cors>




如果部署到服务器,项目地址会用ip或者域名,这时我们需要修改docapi下的index.html

,把如下图标记的地方改成访问的ip或者域名,不然会抛出:(如图)

Can’t read from server. It may not have the appropriate access-control-origin settings



汉化

如下是我的汉化的目录,解压就即可使用,地址如下:

docapi汉化包下载地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息