1 工具
- swagger
- 下载:https://github.com/swagger-api/swagger-ui
2 配置
2.1 pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
2.2 接口配置类
- ApplicationShow.java
package com.document;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
// import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.service.ApiInfo;
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages="com.controller.bigdata")
// public class ApplicationShow extends WebMvcConfigurationSupport{
public class ApplicationShow{
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("接口文档")
.description("接口可视化")
.version("1.0.0")
.termsOfServiceUrl("")
.license("")
.licenseUrl("")
.build();
}
}
2.3 静态文件
- 路径:新建文件夹/api-document/version2
/webapp/api-document/version2 - github的dist文件夹中的全部文件拷贝至version2文件夹下
- 映射配置
<!--配置接口类-->
<bean class="com.document.ApplicationShow"/>
<!--静态文件-->
<mvc:resources location="/api-document/" mapping="/api-document/**"/>
2.4 url配置
/webapp/api-document/version2/index.html
url:http://localhost:8080/{artifactId}/v2/api-docs
2.5 controller配置
package com.controller.bigdata;
import java.util.Map;
import ***.po.className;
import ***.service.classNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Controller
@RequestMapping("/api")
@Api(value="/api", tags="功能测试", description="数据搜索性能分析")
public class TestController{
@Autowired
private DeviceStatusService deviceStatusService;
@RequestMapping(value="/map-return", method=RequestMethod.POST)
@ResponseBody
@ApiOperation(value="测试接口连接")
public Map mapReturn(@RequestBody Map infos){
return infos;
}
}
2.6 中文
/webapp/api-document/version2/index.html
<script src='lang/translator.js' type='text/javascript'></script>
<script src='lang/zh-cn.js' type='text/javascript'></script>
3 接口界面
【参考文献】
[1]https://segmentfault.com/a/1190000010465989
[2]https://www.cnblogs.com/jtlgb/p/6734177.html
[3]https://blog.csdn.net/qq_42291606/article/details/87808052
[4]https://github.com/swagger-api/swagger-ui