接口文档
2 分钟 5313 字 |
接口文档
Swagger UI
依赖
<dependency>
<!-- 接口文档 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.22</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.22</version>
</dependency>
接口配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//在properties文件中配置是否开启
@Value("${springfox.documentation.swagger-ui.enabled}")
private Boolean enabled;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(enabled)
.select()
.apis(RequestHandlerSelectors.basePackage("com.dec.simulation.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("后端REST接口")
.description("")
.version("1.0")
.build();
}
}
application.properties
#swagger,使可用
springfox.documentation.swagger-ui.enabled=true
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
MVC配置类
@Configuration
public class MyWebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> converter : converters) {
// 解决 Controller 返回普通文本中文乱码问题
if (converter instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8);
}
// 解决 Controller 返回json对象中文乱码问题
if (converter instanceof MappingJackson2HttpMessageConverter) {
((MappingJackson2HttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8);
}
}
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
//指向外部目录
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
标注
- 实体类标注
@ApiModel("模型")
public class Model {
//Tag
private String modelName;
//mappingPath or inferencePath;
private String pathType;
//Metrics
private String pathUrl;
}
- 控制器标注
@Api(tags = "模型管理")
@RestController
@RequestMapping("/model")
public class ModelController {
@ApiOperation("根据id获取单条步骤详情")
@RequestMapping(value = "/getStep/{iid}", method = RequestMethod.GET)
public Step getStepById(@ApiParam("路径请求:/step/getStep/1 查询id为1的数据") @PathVariable("iid") String iid) {
return stepService.getStepById(iid);
}
}
访问
http://localhost/swagger-ui.html#/
Apifox
swagger-ui有许多不完善的地方,比如界面是英文,接口文档层次不清晰,可以将swagger-ui导入到Apifox中,统一进行接口管理,同时可将接口文档通过外链的方式分享出去
项目设置-数据管理-导入数据(自动同步)
填入swagger-ui的接口地址即可
~ ~ The End ~ ~
分类标签:技术,springboot,java
文章标题:接口文档
文章链接:http://120.46.217.131:82/archives/40/
最后编辑:2022 年 9 月 23 日 14:48 By Yang
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
文章标题:接口文档
文章链接:http://120.46.217.131:82/archives/40/
最后编辑:2022 年 9 月 23 日 14:48 By Yang
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)