开场白
上一期已经把spirng-cloud简单项目建起来了,那么按照这个项目作为基础模板,继续往上加点东西。加点什么好了?自然是越通用的越好,那么就选定用knife4j吧。可以自动化生成api文档还可以直接测试调用,更方便了。
上一期的项目链接
https://www.jianshu.com/p/0e2e55d0ccde
新建一个module
spring-cloud-knife4j
pom配置
把client的pom配置复制过去,再加上knife4j的依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.lu</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>spring-cloud-knife4j</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>${springcloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${springcloud.version}</version>
</dependency>
<!-- eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${eureka.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.5.0</version>
</dependency>
</dependencies>
</project>
主程序
加上把api文档链接的输出,方便直接跳转
package com.lu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class App {
static Logger logger= LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
ConfigurableApplicationContext application= SpringApplication.run(App.class, args);
Environment env = application.getEnvironment();
logger.info("\n----------------------------------------------------------\n\t" +
"Application is running! Access URLs:\n\t" +
"Local: \t\thttp://localhost:{}\n\t" +
"External: \thttp://{}:{}\n\t"+
"swagger: \thttp://{}:{}/doc.html\n"+
"----------------------------------------------------------",
env.getProperty("local.server.port"),
env.getProperty("spring.cloud.client.hostname"),
env.getProperty("local.server.port"),
env.getProperty("spring.cloud.client.hostname"),
env.getProperty("local.server.port")
);
}
}
application.yml
加上springdoc-openapi的配置
spring:
application:
name: cloud-knife4j
port: 9002
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: true
fetch-registry: true
# springdoc-openapi项目配置
springdoc:
swagger-ui:
path: /swagger-ui.html
tags-sorter: alpha
operations-sorter: alpha
api-docs:
path: /v3/api-docs
group-configs:
- group: 'default'
paths-to-match: '/**'
测试用的controller
package com.lu;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@RestController
public class SpringContoller {
@Operation(summary = "ping")
@RequestMapping(value = "/ping", method = GET)
public String ping(){
return "ping success";
}
}
万事俱备,启动调试
启动输出
----------------------------------------------------------
Application is running! Access URLs:
Local: http://localhost:8080
External: http://host.docker.internal:8080
swagger: http://host.docker.internal:8080/doc.html
----------------------------------------------------------
访问http://host.docker.internal:8080/doc.html
api doc页面
测试调用
测试调用
成功,大功告成
git项目链接:
https://gitee.com/ice_moon_cake/spring-cloud-simple/tree/master/spring-cloud/spring-cloud-knife4j