调整:优化eps解析规则,兼容 如 AdminMarketCouponInfoController,解析为 /admin/market/coupon/info 无需再admin下多个 coupon层包
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
package com.cool.core.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 自定义路由注解
|
||||
*/
|
||||
@@ -23,4 +22,13 @@ public @interface CoolRestController {
|
||||
String[] value() default {};
|
||||
|
||||
String[] api() default {};
|
||||
|
||||
/**
|
||||
* 如前缀: /admin/goods/searchKeyword
|
||||
* 没指定该字段 cname="searchKeyword",
|
||||
* 按规则是解析为: /admin/goods/search/keyword
|
||||
* 前端和node版本已经定义为 searchKeyword,没按规则解析,使用该字段自定义规则 进行兼容
|
||||
* com.cool.core.request.prefix.AutoPrefixUrlMapping#getCName(java.lang.Class, java.lang.String)
|
||||
*/
|
||||
String cname() default "";
|
||||
}
|
||||
|
||||
@@ -2,14 +2,15 @@ package com.cool.core.request.prefix;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import com.cool.core.annotation.CoolRestController;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import com.cool.core.util.ConvertUtil;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
/**
|
||||
* 自动配置模块的路由
|
||||
@@ -23,12 +24,12 @@ public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {
|
||||
RequestMappingInfo info = super.getMappingForMethod(method, handlerType);
|
||||
String packageName = handlerType.getPackage().getName();
|
||||
if (info != null && annotations.length > 0 && annotations[0].value().length == 0
|
||||
&& packageName.contains("modules")) {
|
||||
&& packageName.contains("modules")) {
|
||||
if (!checkApis(annotations, info)) {
|
||||
return null;
|
||||
}
|
||||
String prefix = getPrefix(handlerType, packageName);
|
||||
String cName = getCName(handlerType, prefix);
|
||||
String prefix = getPrefix(packageName);
|
||||
String cName = getCName(annotations[0].cname(), handlerType, prefix);
|
||||
info = info.mutate().paths(prefix + "/" + cName).build().combine(info);
|
||||
}
|
||||
return info;
|
||||
@@ -69,21 +70,25 @@ public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {
|
||||
* @param prefix 路由前缀
|
||||
* @return url地址
|
||||
*/
|
||||
private String getCName(Class<?> handlerType, String prefix) {
|
||||
private String getCName(String cname, Class<?> handlerType, String prefix) {
|
||||
if (ObjUtil.isNotEmpty(cname)) {
|
||||
return cname;
|
||||
}
|
||||
String name = handlerType.getName();
|
||||
String[] names = name.split("[.]");
|
||||
name = names[names.length - 1];
|
||||
return name.toLowerCase().replace("controller", "").replace(prefix.replace("/", ""), "");
|
||||
cname = name.replace(ConvertUtil.pathToClassName(prefix), "")
|
||||
.replace("Controller", "");
|
||||
return ConvertUtil.classNameToPath(cname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建路由前缀
|
||||
*
|
||||
* @param handlerType 类
|
||||
* @param packageName 包名
|
||||
* @return 返回路由前缀
|
||||
*/
|
||||
private String getPrefix(Class<?> handlerType, String packageName) {
|
||||
private String getPrefix(String packageName) {
|
||||
String dotPath = packageName.split("modules")[1]; // 将包路径中多于的部分截取掉
|
||||
String[] dotPaths = dotPath.replace(".controller", "").split("[.]");
|
||||
List<String> paths = CollUtil.toList(dotPaths);
|
||||
@@ -96,5 +101,4 @@ public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {
|
||||
dotPath = "/" + CollUtil.join(paths, "/");
|
||||
return dotPath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.cool.core.util;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
@@ -149,4 +150,38 @@ public class ConvertUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* /admin/goods 转 AdminGoods
|
||||
*/
|
||||
public static String pathToClassName(String path) {
|
||||
// 按斜杠分割字符串
|
||||
String[] parts = path.split("/");
|
||||
StringBuilder className = new StringBuilder();
|
||||
for (String part : parts) {
|
||||
// 将每个部分的首字母大写,并追加到 StringBuilder 中
|
||||
className.append(StrUtil.upperFirst(part));
|
||||
}
|
||||
return className.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* CouponInfo 转 coupon/info
|
||||
*/
|
||||
public static String classNameToPath(String className) {
|
||||
StringBuilder path = new StringBuilder();
|
||||
for (char c : className.toCharArray()) {
|
||||
if (Character.isUpperCase(c)) {
|
||||
if (!path.isEmpty()) {
|
||||
path.append("/");
|
||||
}
|
||||
path.append(Character.toLowerCase(c));
|
||||
} else {
|
||||
path.append(c);
|
||||
}
|
||||
}
|
||||
return path.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user