swagger用于定义API文档。
源码地址:
https://github.com/tanzj520/SwaggerApiDemo
好处:
- 前后端分离开发 API文档非常明确
- 测试的时候不需要再使用URL输入浏览器的方式来访问Controller
- 传统的输入URL的测试方式对于post请求的传参比较麻烦
其实也可以选择第三方工具 https://www.eolinker.com/
首先,在项目pom中引入依赖,如下(我使用的2.8.0版本)
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
接着,在SpringBoot中创建Application.java,如下(我这里使用的外置tomcat 并没有使用spring boot自带的)
package com.ktnw.management;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication //spring boot 应用
@EnableTransactionManagement //事务管理
@EnableSwagger2 //swagger 注解
public class SwaggerApiDemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder con