制作原因:我们每次写完接口还需要花费时间写接口的说明文档给前端用来参考,或是写完接口后需要调试,我们要在postman输入url,参数等信息。这些都是比较麻烦的事情,但是如果我们在idea中集成了Swagger后,写完接口,直接启动,把网址发给前端,前端就能拿到我们所有的接口文档,我们也可以直接在Swagger里进行接口的调试。
制作方法:
第一步先在pom文件里导入Swagger的依赖
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.8</version>
</dependency>
第二步在yml中配置,我这里用的是application.properties
server.port=8060
#你的数据库信息
spring.datasource.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true
#账号
spring.datasource.username=root
#密码
spring.datasource.password=root
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
第三步,在启动类的同级包下,创建config包,在config包下创建三个类
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 通过.select()方法,配置扫描哪些API接口,RequestHandlerSelectors配置如何扫描接口
.select()
//any() // 扫描所有,项目中的所有接口都会被扫描到
//none() // 不扫描接口
// 通过方法上的注解扫描,如withMethodAnnotation(GetMapping.class)只扫描get请求
//withMethodAnnotation(final Class<? extends Annotation> annotation)
// 通过类上的注解扫描,如.withClassAnnotation(Controller.class)只扫描有controller注解的类中的接口
//withClassAnnotation(final Class<? extends Annotation> annotation)
//basePackage(final String basePackage) // 根据包路径扫描接口
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //添加ApiOperiation注解的被扫描
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
Contact contact = new Contact("name", "url", "***@163.com");
return new ApiInfo(
"项目名",
"项目描述",
"版本1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
第二个,这个类的作用是每次启动服务时打印出Swagger的网址,可以根据自己的选择进行导入
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import java.net.Inet4Address;
import java.net.UnknownHostException;
@Component
@Slf4j
public class SwaggerPrintConfig implements ApplicationListener<WebServerInitializedEvent> {
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
try {
// 获取IP
String hostAddress = Inet4Address.getLocalHost().getHostAddress();
// 获取端口号
int port = event.getWebServer().getPort();
// 获取应用名
String applicationName = event.getApplicationContext().getApplicationName();
// 打印 swagger 文档地址
//log.info("项目启动启动成功!swagger 接口文档地址: http://" + hostAddress + ":" + port + applicationName + "/swagger-ui.html");
// 打印 swagger2 文档地址
log.info("项目启动启动成功!swagger 接口文档地址: http://" + hostAddress + ":" + port + applicationName + "/doc.html");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
第三个
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
/** 配置knife4j 显示文档 */
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
/**
* 配置swagger-ui显示文档
*/
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
/** 公共部分内容 */
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
此时启动项目,可以根据打印的网址访问Swagger,也可以自己去访问http://localhost:8080/doc.html
之后稍微麻烦的就是写controller层时要在类上加注解
@Api(value = "Controller", tags = { "获取相关接口" })
方法上加注解
@ApiOperation(value = "获取方面作用")
在entiy层的类加注解
@ApiModel(value = "用户类Model")
成员变量上加注解
@ApiModelProperty(value = "ID")
这样就完成了,前端不需要写接口文档也能知道字段的映射
当然这种写注解的方式还是比较麻烦,这个时候就需要集成另一个自动生成代码的工具了,我们根据数据库表直接生成该表的增删改查以及上述的注解。这块我会在之后的文章里写出来,希望大家能关注我。创作不易,如果对你有用,请点个赞哦