Skip to content

Commit 15f8706

Browse files
committed
refactor(extension/crud): 增强 BaseController 内 API 校验,支持指定 Controller 单独处理
1 parent 3a0c3e0 commit 15f8706

File tree

7 files changed

+114
-27
lines changed

7 files changed

+114
-27
lines changed

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/aop/CrudApiAnnotationInterceptor.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import org.springframework.core.annotation.AnnotatedElementUtils;
2424
import org.springframework.util.ClassUtils;
2525
import top.continew.starter.extension.crud.annotation.CrudApi;
26+
import top.continew.starter.extension.crud.controller.BaseController;
2627
import top.continew.starter.extension.crud.handler.CrudApiHandler;
28+
import top.continew.starter.extension.crud.handler.CrudApiStrategy;
2729

2830
import java.lang.reflect.Method;
2931
import java.util.Objects;
@@ -36,12 +38,6 @@
3638
*/
3739
public class CrudApiAnnotationInterceptor implements MethodInterceptor {
3840

39-
private final CrudApiHandler handler;
40-
41-
public CrudApiAnnotationInterceptor(CrudApiHandler handler) {
42-
this.handler = handler;
43-
}
44-
4541
@Override
4642
public Object invoke(MethodInvocation invocation) throws Throwable {
4743
// 获取目标类
@@ -51,7 +47,13 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
5147
Method targetMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
5248
// 获取 @CrudApi 注解
5349
CrudApi crudApi = AnnotatedElementUtils.findMergedAnnotation(targetMethod, CrudApi.class);
54-
handler.preHandle(crudApi, targetMethod, targetClass);
50+
// 获取处理器
51+
CrudApiHandler<?> crudApiHandler = CrudApiStrategy.INSTANCE.handlerMap.get(targetClass);
52+
if (crudApiHandler != null) {
53+
crudApiHandler.preHandle(crudApi, targetMethod, targetClass);
54+
} else {
55+
CrudApiStrategy.INSTANCE.handlerMap.get(BaseController.class).preHandle(crudApi, targetMethod, targetClass);
56+
}
5557
return invocation.proceed();
5658
}
5759
}

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/autoconfigure/CrudRequestMappingHandlerMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected RequestMappingInfo getMappingForMethod(@NonNull Method method, @NonNul
5353
// 过滤 API,如果非本类中定义,且 API 列表中不包含,则忽略
5454
Api[] apiArr = crudRequestMapping.api();
5555
Api api = ExceptionUtils.exToNull(() -> Api.valueOf(method.getName().toUpperCase()));
56-
if (method.getDeclaringClass() != handlerType && !ArrayUtil.containsAny(apiArr, Api.ALL, api)) {
56+
if (method.getDeclaringClass() != handlerType && !ArrayUtil.contains(apiArr, api)) {
5757
return null;
5858
}
5959
// 拼接路径(合并了 @RequestMapping 的部分能力)

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/autoconfigure/CrudRestControllerAutoConfiguration.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import jakarta.annotation.PostConstruct;
2020
import org.slf4j.Logger;
2121
import org.slf4j.LoggerFactory;
22+
import org.springframework.beans.factory.annotation.Autowired;
2223
import org.springframework.boot.autoconfigure.AutoConfiguration;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2425
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -27,7 +28,9 @@
2728
import top.continew.starter.extension.crud.aop.CrudApiAnnotationAdvisor;
2829
import top.continew.starter.extension.crud.aop.CrudApiAnnotationInterceptor;
2930
import top.continew.starter.extension.crud.handler.CrudApiHandler;
30-
import top.continew.starter.extension.crud.handler.DefaultCrudApiHandler;
31+
import top.continew.starter.extension.crud.handler.CrudApiStrategy;
32+
33+
import java.util.List;
3134

3235
/**
3336
* CRUD REST Controller 自动配置
@@ -42,30 +45,33 @@ public class CrudRestControllerAutoConfiguration {
4245
private static final Logger log = LoggerFactory.getLogger(CrudRestControllerAutoConfiguration.class);
4346

4447
/**
45-
* CRUD API 注解通知
48+
* 注入自定义处理器
49+
*
50+
* @param handlerList 自定义处理器集合
4651
*/
47-
@Bean
48-
@ConditionalOnMissingBean
49-
public CrudApiAnnotationAdvisor crudApiAnnotationAdvisor(CrudApiAnnotationInterceptor crudApiAnnotationInterceptor) {
50-
return new CrudApiAnnotationAdvisor(crudApiAnnotationInterceptor, CrudApi.class);
52+
@Autowired(required = false)
53+
public void setCrudApiHandler(List<CrudApiHandler<?>> handlerList) {
54+
for (CrudApiHandler<?> handler : handlerList) {
55+
CrudApiStrategy.INSTANCE.registerHandler(handler);
56+
}
5157
}
5258

5359
/**
54-
* CRUD API 注解拦截器
60+
* CRUD API 注解通知
5561
*/
5662
@Bean
5763
@ConditionalOnMissingBean
58-
public CrudApiAnnotationInterceptor crudApiAnnotationInterceptor(CrudApiHandler crudApiHandler) {
59-
return new CrudApiAnnotationInterceptor(crudApiHandler);
64+
public CrudApiAnnotationAdvisor crudApiAnnotationAdvisor(CrudApiAnnotationInterceptor crudApiAnnotationInterceptor) {
65+
return new CrudApiAnnotationAdvisor(crudApiAnnotationInterceptor, CrudApi.class);
6066
}
6167

6268
/**
63-
* CRUD API 处理器(默认)
69+
* CRUD API 注解拦截器
6470
*/
6571
@Bean
6672
@ConditionalOnMissingBean
67-
public CrudApiHandler crudApiHandler() {
68-
return new DefaultCrudApiHandler();
73+
public CrudApiAnnotationInterceptor crudApiAnnotationInterceptor() {
74+
return new CrudApiAnnotationInterceptor();
6975
}
7076

7177
@PostConstruct

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/enums/Api.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
*/
2525
public enum Api {
2626

27-
/**
28-
* 所有 API
29-
*/
30-
ALL,
31-
3227
/**
3328
* 分页
3429
*/

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/handler/CrudApiHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package top.continew.starter.extension.crud.handler;
1818

1919
import top.continew.starter.extension.crud.annotation.CrudApi;
20+
import top.continew.starter.extension.crud.controller.BaseController;
2021

2122
import java.lang.reflect.Method;
2223

@@ -26,7 +27,14 @@
2627
* @author Charles7c
2728
* @since 2.7.5
2829
*/
29-
public interface CrudApiHandler {
30+
public interface CrudApiHandler<T extends BaseController> {
31+
32+
/**
33+
* 获取处理器控制器类
34+
*
35+
* @return 处理器控制器类
36+
*/
37+
Class<T> getHandlerControllerClass();
3038

3139
/**
3240
* 前置处理
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.extension.crud.handler;
18+
19+
import top.continew.starter.extension.crud.controller.BaseController;
20+
21+
import java.util.LinkedHashMap;
22+
import java.util.Map;
23+
24+
/**
25+
* CRUD API 策略
26+
*
27+
* @author Charles7c
28+
* @since 2.7.5
29+
*/
30+
public final class CrudApiStrategy {
31+
32+
/**
33+
* 全局单例引用
34+
*/
35+
public static final CrudApiStrategy INSTANCE = new CrudApiStrategy();
36+
37+
/**
38+
* 处理器集合
39+
*/
40+
public Map<Class<?>, CrudApiHandler<?>> handlerMap = new LinkedHashMap<>();
41+
42+
private CrudApiStrategy() {
43+
registerDefaultHandler();
44+
}
45+
46+
/**
47+
* 注册所有默认的处理器
48+
*/
49+
public void registerDefaultHandler() {
50+
handlerMap.put(BaseController.class, new DefaultCrudApiHandler());
51+
}
52+
53+
/**
54+
* 注册一个处理器
55+
*
56+
* @param handler 处理器
57+
*/
58+
public void registerHandler(CrudApiHandler<?> handler) {
59+
handlerMap.put(handler.getHandlerControllerClass(), handler);
60+
}
61+
62+
/**
63+
* 移除一个注解处理器
64+
*
65+
* @param controllerClass 控制器类
66+
*/
67+
public void removeAnnotationHandler(Class<?> controllerClass) {
68+
handlerMap.remove(controllerClass);
69+
}
70+
}

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/handler/DefaultCrudApiHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package top.continew.starter.extension.crud.handler;
1818

1919
import top.continew.starter.extension.crud.annotation.CrudApi;
20+
import top.continew.starter.extension.crud.controller.BaseController;
2021

2122
import java.lang.reflect.Method;
2223

@@ -26,7 +27,12 @@
2627
* @author Charles7c
2728
* @since 2.7.5
2829
*/
29-
public class DefaultCrudApiHandler implements CrudApiHandler {
30+
public class DefaultCrudApiHandler implements CrudApiHandler<BaseController> {
31+
32+
@Override
33+
public Class getHandlerControllerClass() {
34+
return BaseController.class;
35+
}
3036

3137
@Override
3238
public void preHandle(CrudApi crudApi, Method targetMethod, Class<?> targetClass) {

0 commit comments

Comments
 (0)