knife4j升级4.3.0注解改造

This commit is contained in:
EightMonth
2023-12-27 11:05:28 +08:00
parent 82a2a676a5
commit 21b031cc21
91 changed files with 865 additions and 893 deletions

View File

@ -1,160 +1,160 @@
package org.jeecg.handler.swagger;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import java.util.*;
/**
* 聚合各个服务的swagger接口
* @author zyf
* @date: 2022/4/21 10:55
*/
@Component
@Slf4j
@Primary
public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
/**
* swagger2默认的url后缀
*/
private static final String SWAGGER2URL = "/v2/api-docs";
/**
* 网关路由
*/
private final RouteLocator routeLocator;
/**
* Nacos名字服务
*/
private NamingService naming;
/**
* nacos服务地址
*/
@Value("${spring.cloud.nacos.discovery.server-addr}")
private String serverAddr;
/**
* nacos namespace
*/
@Value("${spring.cloud.nacos.discovery.namespace:#{null}}")
private String namespace;
/**
* nacos groupName
*/
@Value("${spring.cloud.nacos.config.group:DEFAULT_GROUP:#{null}}")
private String group;
/**
* nacos username
*/
@Value("${spring.cloud.nacos.discovery.username:#{null}}")
private String username;
/**
* nacos password
*/
@Value("${spring.cloud.nacos.discovery.password:#{null}}")
private String password;
/**
* Swagger中需要排除的服务
*/
private String[] excludeServiceIds=new String[]{"jeecg-cloud-monitor"};
/**
* 网关应用名称
*/
@Value("${spring.application.name}")
private String self;
@Autowired
public MySwaggerResourceProvider(RouteLocator routeLocator) {
this.routeLocator = routeLocator;
}
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
List<String> routeHosts = new ArrayList<>();
// 获取所有可用的hostserviceId
routeLocator.getRoutes().filter(route -> route.getUri().getHost() != null)
.filter(route -> !self.equals(route.getUri().getHost()))
.subscribe(route ->{
//update-begin---author:zyf ---date:20220413 for过滤掉无效路由,避免接口文档报错无法打开
boolean hasRoute=checkRoute(route.getId());
if(hasRoute){
routeHosts.add(route.getUri().getHost());
}
//update-end---author:zyf ---date:20220413 for过滤掉无效路由,避免接口文档报错无法打开
});
// 记录已经添加过的server存在同一个应用注册了多个服务在nacos上
Set<String> dealed = new HashSet<>();
routeHosts.forEach(instance -> {
// 拼接url
String url = "/" + instance.toLowerCase() + SWAGGER2URL;
if (!dealed.contains(url)) {
dealed.add(url);
log.info(" Gateway add SwaggerResource: {}",url);
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setUrl(url);
swaggerResource.setSwaggerVersion("2.0");
swaggerResource.setName(instance);
//Swagger排除不展示的服务
if(!ArrayUtil.contains(excludeServiceIds,instance)){
resources.add(swaggerResource);
}
}
});
return resources;
}
/**
* 检测nacos中是否有健康实例
* @param routeId
* @return
*/
private Boolean checkRoute(String routeId) {
Boolean hasRoute = false;
try {
//修复使用带命名空间启动网关swagger看不到接口文档的问题
Properties properties=new Properties();
properties.setProperty("serverAddr",serverAddr);
if(namespace!=null && !"".equals(namespace)){
log.info("nacos.discovery.namespace = {}", namespace);
properties.setProperty("namespace",namespace);
}
if(username!=null && !"".equals(username)){
properties.setProperty("username",username);
}
if(password!=null && !"".equals(password)){
properties.setProperty("password",password);
}
//【issues/5115】因swagger文档导致gateway内存溢出
if (this.naming == null) {
this.naming = NamingFactory.createNamingService(properties);
}
log.info(" config.group : {}", group);
List<Instance> list = this.naming.selectInstances(routeId, group , true);
if (ObjectUtil.isNotEmpty(list)) {
hasRoute = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return hasRoute;
}
}
//package org.jeecg.handler.swagger;
//
//import cn.hutool.core.util.ArrayUtil;
//import cn.hutool.core.util.ObjectUtil;
//import com.alibaba.nacos.api.naming.NamingFactory;
//import com.alibaba.nacos.api.naming.NamingService;
//import com.alibaba.nacos.api.naming.pojo.Instance;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.gateway.route.RouteLocator;
//import org.springframework.context.annotation.Primary;
//import org.springframework.stereotype.Component;
//
//import springfox.documentation.swagger.web.SwaggerResource;
//import springfox.documentation.swagger.web.SwaggerResourcesProvider;
//
//import java.util.*;
//
///**
// * 聚合各个服务的swagger接口
// * @author zyf
// * @date: 2022/4/21 10:55
// */
//@Component
//@Slf4j
//@Primary
//public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
// /**
// * swagger2默认的url后缀
// */
// private static final String SWAGGER2URL = "/v2/api-docs";
//
// /**
// * 网关路由
// */
// private final RouteLocator routeLocator;
// /**
// * Nacos名字服务
// */
// private NamingService naming;
//
// /**
// * nacos服务地址
// */
// @Value("${spring.cloud.nacos.discovery.server-addr}")
// private String serverAddr;
// /**
// * nacos namespace
// */
// @Value("${spring.cloud.nacos.discovery.namespace:#{null}}")
// private String namespace;
//
// /**
// * nacos groupName
// */
// @Value("${spring.cloud.nacos.config.group:DEFAULT_GROUP:#{null}}")
// private String group;
//
// /**
// * nacos username
// */
// @Value("${spring.cloud.nacos.discovery.username:#{null}}")
// private String username;
// /**
// * nacos password
// */
// @Value("${spring.cloud.nacos.discovery.password:#{null}}")
// private String password;
//
// /**
// * Swagger中需要排除的服务
// */
// private String[] excludeServiceIds=new String[]{"jeecg-cloud-monitor"};
//
//
// /**
// * 网关应用名称
// */
// @Value("${spring.application.name}")
// private String self;
//
// @Autowired
// public MySwaggerResourceProvider(RouteLocator routeLocator) {
// this.routeLocator = routeLocator;
// }
//
// @Override
// public List<SwaggerResource> get() {
// List<SwaggerResource> resources = new ArrayList<>();
// List<String> routeHosts = new ArrayList<>();
// // 获取所有可用的hostserviceId
// routeLocator.getRoutes().filter(route -> route.getUri().getHost() != null)
// .filter(route -> !self.equals(route.getUri().getHost()))
// .subscribe(route ->{
// //update-begin---author:zyf ---date:20220413 for过滤掉无效路由,避免接口文档报错无法打开
// boolean hasRoute=checkRoute(route.getId());
// if(hasRoute){
// routeHosts.add(route.getUri().getHost());
// }
// //update-end---author:zyf ---date:20220413 for过滤掉无效路由,避免接口文档报错无法打开
// });
//
// // 记录已经添加过的server存在同一个应用注册了多个服务在nacos上
// Set<String> dealed = new HashSet<>();
// routeHosts.forEach(instance -> {
// // 拼接url
// String url = "/" + instance.toLowerCase() + SWAGGER2URL;
// if (!dealed.contains(url)) {
// dealed.add(url);
// log.info(" Gateway add SwaggerResource: {}",url);
// SwaggerResource swaggerResource = new SwaggerResource();
// swaggerResource.setUrl(url);
// swaggerResource.setSwaggerVersion("2.0");
// swaggerResource.setName(instance);
// //Swagger排除不展示的服务
// if(!ArrayUtil.contains(excludeServiceIds,instance)){
// resources.add(swaggerResource);
// }
// }
// });
// return resources;
// }
//
// /**
// * 检测nacos中是否有健康实例
// * @param routeId
// * @return
// */
// private Boolean checkRoute(String routeId) {
// Boolean hasRoute = false;
// try {
// //修复使用带命名空间启动网关swagger看不到接口文档的问题
// Properties properties=new Properties();
// properties.setProperty("serverAddr",serverAddr);
// if(namespace!=null && !"".equals(namespace)){
// log.info("nacos.discovery.namespace = {}", namespace);
// properties.setProperty("namespace",namespace);
// }
// if(username!=null && !"".equals(username)){
// properties.setProperty("username",username);
// }
// if(password!=null && !"".equals(password)){
// properties.setProperty("password",password);
// }
// //【issues/5115】因swagger文档导致gateway内存溢出
// if (this.naming == null) {
// this.naming = NamingFactory.createNamingService(properties);
// }
// log.info(" config.group : {}", group);
// List<Instance> list = this.naming.selectInstances(routeId, group , true);
// if (ObjectUtil.isNotEmpty(list)) {
// hasRoute = true;
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// return hasRoute;
// }
//}

View File

@ -1,41 +1,41 @@
package org.jeecg.handler.swagger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger.web.*;
import java.util.List;
/**
* swagger聚合接口三个接口都是 doc.html需要访问的接口
* @author zyf
* @date: 2022/4/21 10:55
*/
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerResourceController {
private MySwaggerResourceProvider swaggerResourceProvider;
@Autowired
public SwaggerResourceController(MySwaggerResourceProvider swaggerResourceProvider) {
this.swaggerResourceProvider = swaggerResourceProvider;
}
@RequestMapping(value = "/configuration/security")
public ResponseEntity<SecurityConfiguration> securityConfiguration() {
return new ResponseEntity<>(SecurityConfigurationBuilder.builder().build(), HttpStatus.OK);
}
@RequestMapping(value = "/configuration/ui")
public ResponseEntity<UiConfiguration> uiConfiguration() {
return new ResponseEntity<>(UiConfigurationBuilder.builder().build(), HttpStatus.OK);
}
@RequestMapping
public ResponseEntity<List<SwaggerResource>> swaggerResources() {
return new ResponseEntity<>(swaggerResourceProvider.get(), HttpStatus.OK);
}
}
//package org.jeecg.handler.swagger;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//import springfox.documentation.swagger.web.*;
//
//import java.util.List;
//
///**
// * swagger聚合接口三个接口都是 doc.html需要访问的接口
// * @author zyf
// * @date: 2022/4/21 10:55
// */
//@RestController
//@RequestMapping("/swagger-resources")
//public class SwaggerResourceController {
// private MySwaggerResourceProvider swaggerResourceProvider;
//
// @Autowired
// public SwaggerResourceController(MySwaggerResourceProvider swaggerResourceProvider) {
// this.swaggerResourceProvider = swaggerResourceProvider;
// }
//
// @RequestMapping(value = "/configuration/security")
// public ResponseEntity<SecurityConfiguration> securityConfiguration() {
// return new ResponseEntity<>(SecurityConfigurationBuilder.builder().build(), HttpStatus.OK);
// }
//
// @RequestMapping(value = "/configuration/ui")
// public ResponseEntity<UiConfiguration> uiConfiguration() {
// return new ResponseEntity<>(UiConfigurationBuilder.builder().build(), HttpStatus.OK);
// }
//
// @RequestMapping
// public ResponseEntity<List<SwaggerResource>> swaggerResources() {
// return new ResponseEntity<>(swaggerResourceProvider.get(), HttpStatus.OK);
// }
//}

View File

@ -9,10 +9,9 @@ import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.utils.StringUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.base.BaseMap;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.RedisUtil;
@ -32,6 +31,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import reactor.core.publisher.Mono;
import java.net.URI;
@ -170,9 +170,9 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
dynamicRouteService.add(definition);
}
}
if(ObjectUtils.isNotEmpty(baseMap)){
if(!ObjectUtils.isEmpty(baseMap)){
String delRouterId = baseMap.get("delRouterId");
if (ObjectUtils.isNotEmpty(delRouterId)) {
if (!ObjectUtils.isEmpty(delRouterId)) {
dynamicRouteService.delete(delRouterId);
}
}

View File

@ -1,6 +1,8 @@
package org.jeecg.modules.test.feign.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
@ -8,8 +10,6 @@ import org.jeecg.modules.test.feign.client.JeecgTestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
* 微服务单元测试
@ -19,7 +19,7 @@ import io.swagger.annotations.ApiOperation;
@Slf4j
@RestController
@RequestMapping("/sys/test")
@Api(tags = "【微服务】单元测试")
@Tag(name = "【微服务】单元测试")
public class JeecgTestFeignController {
@Autowired
@ -32,7 +32,7 @@ public class JeecgTestFeignController {
* @return
*/
@GetMapping("/getMessage")
@ApiOperation(value = "测试feign调用demo服务1", notes = "测试feign @SentinelResource熔断写法 | 测试熔断关闭jeecg-demo服务")
@Operation(summary = "测试feign @SentinelResource熔断写法 | 测试熔断关闭jeecg-demo服务")
@SentinelResource(value = "test_more_getMessage", fallback = "getDefaultUser")
public Result<String> getMessage(@RequestParam(value = "name", required = false) String name) {
log.info("---------Feign fallbackFactory优先级高于@SentinelResource-----------------");
@ -47,7 +47,7 @@ public class JeecgTestFeignController {
* @return
*/
@GetMapping("/getMessage2")
@ApiOperation(value = "测试feign调用demo服务2", notes = "测试feign fallbackFactory熔断写法 | 测试熔断关闭jeecg-demo服务")
@Operation(summary = "测试feign fallbackFactory熔断写法 | 测试熔断关闭jeecg-demo服务")
public Result<String> getMessage2(@RequestParam(value = "name", required = false) String name) {
log.info("---------测试 Feign fallbackFactory-----------------");
String resultMsg = jeecgTestClient.getMessage(" I am jeecg-system 服务节点,呼叫 jeecg-demo!");
@ -56,7 +56,7 @@ public class JeecgTestFeignController {
@GetMapping("/fallback")
@ApiOperation(value = "测试熔断", notes = "测试熔断")
@Operation(summary = "测试熔断")
@SentinelResource(value = "test_more_fallback", fallback = "getDefaultUser")
public Result<Object> test(@RequestParam(value = "name", required = false) String name) {
if (StringUtils.isEmpty(name)) {

View File

@ -3,6 +3,8 @@ package org.jeecg.modules.test.rabbitmq.controller;
import javax.servlet.http.HttpServletRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.base.BaseMap;
@ -13,8 +15,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
@ -24,7 +24,7 @@ import io.swagger.annotations.ApiOperation;
*/
@RestController
@RequestMapping("/sys/test")
@Api(tags = "【微服务】MQ单元测试")
@Tag(name = "【微服务】MQ单元测试")
public class JeecgMqTestController {
@Autowired
@ -39,7 +39,7 @@ public class JeecgMqTestController {
* @return
*/
@GetMapping(value = "/rabbitmq")
@ApiOperation(value = "测试rabbitmq", notes = "测试rabbitmq")
@Operation(summary = "测试rabbitmq")
public Result<?> rabbitMqClientTest(HttpServletRequest req) {
//rabbitmq消息队列测试
BaseMap map = new BaseMap();
@ -50,7 +50,7 @@ public class JeecgMqTestController {
}
@GetMapping(value = "/rabbitmq2")
@ApiOperation(value = "rabbitmq消息总线测试", notes = "rabbitmq消息总线测试")
@Operation(summary = "rabbitmq消息总线测试")
public Result<?> rabbitmq2(HttpServletRequest req) {
//rabbitmq消息总线测试

View File

@ -6,9 +6,8 @@ package org.jeecg.modules.test.seata.order.controller;
* @date: 2022/01/24
* @version: V1.0
*/
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
import org.jeecg.modules.test.seata.order.service.SeataOrderService;
import org.springframework.beans.factory.annotation.Autowired;
@ -20,7 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test/seata/order")
@Api(tags = "seata测试")
@Tag(name = "seata测试")
public class SeataOrderController {
@Autowired
@ -30,7 +29,7 @@ public class SeataOrderController {
* 自由下单
*/
@PostMapping("/placeOrder")
@ApiOperation(value = "自由下单", notes = "自由下单")
@Operation(summary = "自由下单")
public String placeOrder(@Validated @RequestBody PlaceOrderRequest request) {
orderService.placeOrder(request);
return "下单成功";
@ -40,7 +39,7 @@ public class SeataOrderController {
* 测试商品库存不足-异常回滚
*/
@PostMapping("/test1")
@ApiOperation(value = "测试商品库存不足", notes = "测试商品库存不足")
@Operation(summary = "测试商品库存不足")
public String test1() {
//商品单价10元库存20个,用户余额50元模拟一次性购买22个。 期望异常回滚
orderService.placeOrder(new PlaceOrderRequest(1L, 1L, 22));
@ -51,7 +50,7 @@ public class SeataOrderController {
* 测试用户账户余额不足-异常回滚
*/
@PostMapping("/test2")
@ApiOperation(value = "测试用户账户余额不足", notes = "测试用户账户余额不足")
@Operation(summary = "测试用户账户余额不足")
public String test2() {
//商品单价10元库存20个用户余额50元模拟一次性购买6个。 期望异常回滚
orderService.placeOrder(new PlaceOrderRequest(1L, 1L, 6));

View File

@ -1,5 +1,7 @@
package org.jeecg.modules.test.sharding.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
@ -10,8 +12,6 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
/**
@ -21,7 +21,7 @@ import lombok.extern.slf4j.Slf4j;
* @version: V1.0
*/
@Slf4j
@Api(tags = "分库分表测试")
@Tag(name = "分库分表测试")
@RestController
@RequestMapping("/sharding")
public class JeecgShardingDemoController extends JeecgController<ShardingSysLog, IShardingSysLogService> {
@ -33,7 +33,7 @@ public class JeecgShardingDemoController extends JeecgController<ShardingSysLog,
* @return
*/
@PostMapping(value = "/test1")
@ApiOperation(value = "单库分表插入", notes = "单库分表")
@Operation(summary = "单库分表")
public Result<?> add() {
log.info("---------------------------------单库分表插入--------------------------------");
int size = 10;
@ -52,7 +52,7 @@ public class JeecgShardingDemoController extends JeecgController<ShardingSysLog,
* @return
*/
@PostMapping(value = "/list1")
@ApiOperation(value = "单库分表查询", notes = "单库分表")
@Operation(summary = "单库分表")
public Result<?> list() {
return Result.OK(shardingSysLogService.list());
}
@ -62,7 +62,7 @@ public class JeecgShardingDemoController extends JeecgController<ShardingSysLog,
* @return
*/
@PostMapping(value = "/test2")
@ApiOperation(value = "分库分表插入", notes = "分库分表")
@Operation(summary = "分库分表")
public Result<?> test2() {
int start=20;
int size=30;
@ -81,7 +81,7 @@ public class JeecgShardingDemoController extends JeecgController<ShardingSysLog,
* @return
*/
@PostMapping(value = "/list2")
@ApiOperation(value = "分库分表查询", notes = "分库分表")
@Operation(summary = "分库分表")
public Result<?> list2() {
return Result.OK(shardingSysLogService.list());
}