本文标识 : Jsbt0004
本文编辑 : Jack 风
编程工具 : IDEA
阅读时长 : 6分钟
::: tip Swagger
描述和测试 API 接口,自动生成 API 接口文档。
:::
为了方便上面的 API 接口调试,我们可以使用:
<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>
yml 配置文件
swagger:
title: SpringBoot学习
description: SpringBoot快速入门
version: 1.0.0
name: JackFeng
url: http://a2data.cn
email: 66860619@qq.com
Swagger 配置文件
/**
* @author: jack[QQ:66860619]
* @create: 2019-11-20
* @description:
*/
@ConfigurationProperties(prefix = "swagger")
@Configuration //必须存在
@EnableSwagger2 //必须存在
@EnableWebMvc //必须存在
@Data //lomlok
//必须存在 扫描的API Controller包
@ComponentScan(basePackages = {"com.jack.demo.controller"})
public class SwaggerConfig{
private String title;
private String description;
private String version;
private String name;
private String url;
private String email;
@Bean
public Docket customDocket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
Contact contact = new Contact(name, url, email);
return new ApiInfoBuilder()
.title(title)
.description(description)
.contact(contact)
.version(version)
.build();
}
}
配置文件中的值到配置类中的注入,在第八章 springboot-08-配置文件的注入中有介绍
MVC 配置
作用:过滤网页静态资源
@Configuration
class WebMvcConfig implements WebMvcConfigurer {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
在控制器 API 上添加接口描述的注解
@RestController
@Api(value = "用户模块")
public class HelloController {
@GetMapping("/hello")
@ApiOperation(value = "测试Swagger")
public String hello(){
return "hello SpringBoot-swagger";
}
}
官网:https://www.webjars.org/
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1-1</version>
</dependency>
通过 jar 包的方式引入了 jQuery
再浏览器中可以通过如下地址访问到
http://localhost:8080/webjars/jquery/3.3.1-1/jquery.js
地址说明:/webjars/jquery/3.3.1-1/jquery.js
对应上面的 webjars/ artifactId/version/文件名
可以到项目中的 jar 包中具体查看。