vue 根据swagger 生成接口_Spring4集成Swagger2简单三步生成api接口文档

本文介绍了如何将Swagger2集成到Spring MVC项目中,通过简单三步实现接口自动化文档。Swagger能替代单元测试,免去手动编写接口文档,并提供模拟HTTP请求功能,确保接口入参出参准确性。集成步骤包括引入依赖、创建配置类以及添加配置文件。
摘要由CSDN通过智能技术生成

swagger的作用是什么?

首先,你以后基本可以告别单元测试了;其次,你不用再写接口文档了,也不需要写完之后再去对文档进行维护了。swagger可以完全模拟http请求,入参出参和实际情况差别几乎为零。

既然这么方便那我们就集成到我们的SpringMVC项目中吧,很简单。

第一步:引入maven依赖

io.springfox

springfox-swagger2

2.6.1

io.springfox

springfox-swagger-ui

2.6.1

com.fasterxml.jackson.core

jackson-core

2.7.5

com.fasterxml.jackson.core

jackson-annotations

2.7.5

com.fasterxml.jackson.core

jackson-databind

2.7.5

第二步:添加一个类  RestApiConfig.java

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.builders.PathSelectors;

import springfox.documentation.builders.RequestHandlerSelectors;

import springfox.documentation.service.ApiInfo;

import springfox.documentation.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**

* * Restful API 访问路径:

* http://IP:port/{context-path}/swagger-ui.html

* eg:http://localhost:8080/swagger-ui.html

* Created by evan on 2018/5/31.

*/

@EnableWebMvc

@EnableSwagger2

@Configuration

public class RestApiConfig {

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

.apis(RequestHandlerSelectors.basePackage("com.evanosc.controller")) // 注意修改此处的包名

.paths(PathSelectors.any())

.build();

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title("接口列表 v1.1.0") // 任意,请稍微规范点

.description("接口测试") // 任意,请稍微规范点

.termsOfServiceUrl("http://localhost:8080/swagger-ui.html") // 将“url”换成自己的ip:port

.contact("evan") // 无所谓(这里是作者的别称)

.version("1.1.0")

.build();

}

}

第三步:添加配置,一般是dispatcher-servlet.xml

OK,自动生成api接口文档就是这么简单,感觉就是前后端分离的利器。

http://localhost:8080/swagger-ui.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值