Springaop JoinPoint Arguments Sangerser с выражением SPEJAVA

Программисты JAVA общаются здесь
Anonymous
Springaop JoinPoint Arguments Sangerser с выражением SPE

Сообщение Anonymous »

Я создаю аннотацию, как ниже < /p>
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
String template();
String[] parameters() default {};
}
< /code>
Я хочу, чтобы он был общим, поэтому я могу использовать его с вложенными объектами или простыми целыми числами. < /p>
Я буду использовать его поверх Методы, такие как < /p>
@Annotation(
template = "Test-{0}-{1}-{2}",
parameters = {"#request.playroundUid.fundraiserId", "#request.selectionPlayroundNumber", "#request.playroundUid.playroundType"}
)
public TestResponse test(Request request)
< /code>
Поэтому я создал аспект, который использует метод ниже < /p>
public static String generateName(ProceedingJoinPoint joinPoint, Annotation annotation) {
String template = annotation.template();
Object[] args = joinPoint.getArgs();
String[] parameters = annotation.parameters();

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String[] parameterNames = signature.getParameterNames();

// Create a map of parameter names -> values
Map paramMap = new HashMap();
for (int i = 0; i < parameterNames.length; i++) {
paramMap.put(parameterNames, args);
}

// DEBUG: Print out the map to verify correct parameter storage
System.out.println("Parameter Map: " + paramMap);

// SpEL context
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariables(paramMap); // Bind method parameters (e.g., request)

ExpressionParser parser = new SpelExpressionParser();

for (int i = 0; i < parameters.length; i++) {
String parameter = parameters;
if (parameter.startsWith("#")) {
try {
String expression = parameter.substring(1); // Remove "#"

// DEBUG: Print out the expression being evaluated
System.out.println("Evaluating SpEL expression: " + expression);

// Evaluate the SpEL expression directly
Object evaluatedValue = parser.parseExpression(expression).getValue(context);

if (evaluatedValue == null) {
throw new RuntimeException("SpEL expression '" + parameter + "' evaluated to null. Check field access.");
}

template = template.replace("{" + i + "}", evaluatedValue.toString());
} catch (Exception e) {
throw new RuntimeException("Failed to evaluate SpEL expression: " + parameter, e);
}
} else {
template = template.replace("{" + i + "}", args != null ? args.toString() : "null");
}
}
return template;
}
< /code>
Но строка ниже не может проанализировать объект запроса.parser.parseExpression(expression).getValue(context);
< /code>
java.lang.RuntimeException: Failed to evaluate SpEL expression: #request.playroundUid.fundraiserId
at com.novamedia.nl.beehive.participationadministration.util.AnnotationUtil.generateName(AnnotationUtil.java:112)
at com.novamedia.nl.beehive.participationadministration.aspect.AspectTest.shouldGenerateCorrectNameForComplexParameters(AspectTest.java:194)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596) Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'request' cannot be found on null
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:225)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:100)
at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:60)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:96)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:114)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:273)
at com.novamedia.nl.beehive.participationadministration.util.AnnotationUtil.generateLockName(AnnotationUtil.java:104)
... 4 more
< /code>
Вот мой модульный тест < /p>
@Test
void shouldGenerateCorrectNameForComplexParameters() throws NoSuchMethodException {
// Arrange
Annotation annotation = mock(Annotation.class);
when(annotation.template()).thenReturn("CreateTicketsForPlayround-{0}-{1}-{2}");
when(annotation.parameters()).thenReturn(new String[]{"#request.playroundUid.fundraiserId", "#request.selectionPlayroundNumber", "#request.playroundUid.playroundType"});

Method mockMethod = MyTestService.class.getMethod("createTicketsForPlayround", CreateTicketsForPlayroundRequest.class);
when(methodSignature.getMethod()).thenReturn(mockMethod);
when(methodSignature.getParameterNames()).thenReturn(new String[]{"request"});

// Create a mock request with nested properties
CreateTicketsForPlayroundRequest request = CreateTicketsForPlayroundRequest.builder()
.selectionPlayroundNumber(1)
.playroundUid(new PlayroundUid(1001, 1, PlayroundType.REGULAR, 1))
.build();

when(joinPoint.getArgs()).thenReturn(new Object[]{request});

// Act
String name = generateName(joinPoint, annotation);

// Assert
assertEquals("CreateTicketsForPlayround-1001-1-REGULAR", name);
}
< /code>
Кратко, мои вопросы; Как я могу проанализировать запрос объектов с помощью экспрессии SPEL?
Какова наилучшая практика для такого случая? Должен ли я использовать выражения Spel или есть другой способ обработки?


Подробнее здесь: https://stackoverflow.com/questions/794 ... expression

Вернуться в «JAVA»