1.导入jar包:
<!-- Swagger api文档生成相关jar包 -->
<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>
2.编写 Swgger2 配置文件:
package com.wanshi.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
// Swagger 配置类
@Configuration
@EnableSwagger2
public class Swagger_Configuration {
/**
* 配置 Swagger 2
* 注册一个 Bean 属性
* enable():是否启用 Swagger,启用后才能在浏览器中进行访问
* groupName():用于配置 API 文档的分组
*/
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(true)
.groupName("v1")
.select()
// 过滤路径
//.paths(PathSelectors.ant())
// 指定扫描的包
.apis(RequestHandlerSelectors.basePackage("com.wanshi.controller"))
.build();
}
private ApiInfo apiInfo() {
/*作者信息*/
Contact contact = new Contact("K", null, null);
return new ApiInfo(
"Swagger 测试接口文档",
"Spring Boot 集成 Swagger 测试接口文档",
"v1.0",
"https://cunyu1943.github.io",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}
3.编写测试接口:
package com.wanshi.controller;
import com.wanshi.quartz.utils.QuartzUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@Api
@RestController
public class testController {
// @Resource
// private
@Resource
private Scheduler scheduler;
@ApiOperation("删除定时任务,参数为 定时任务 名称")
@PostMapping("/test/{name}")
public String test(@PathVariable String name){
QuartzUtil.deleteScheduleJob(scheduler,name);
return "创建成功,时间:3秒、5秒、10秒";
}
@ApiOperation("hello接口,无参")
@GetMapping("/hello")
public String hello(){
return "hello world";
}
}
可打开在线文档页面
点击接口可以查看详情
点击方法可以查看请求详情
点击右上方的Try it out可以测试接口,请求接口无参数不需要写,如果有,则将参数写入红框内