Springaop Joinpoint Argumente Parser mit Spel AusdruckJava

Java-Forum
Anonymous
 Springaop Joinpoint Argumente Parser mit Spel Ausdruck

Post by Anonymous »

Ich erstelle eine Annotation, wie unten < /p>
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
String template();
String[] parameters() default {};
}
< /code>
Ich möchte Methoden wie < /p>
@Annotation(
template = "Test-{0}-{1}-{2}",
parameters = {"#request.playroundUid.fundraiserId", "#request.selectionPlayroundNumber", "#request.playroundUid.playroundType"}
)
public TestResponse test(Request request)
< /code>
Ich habe also einen Aspekt erstellt, der die folgende Methode verwendet < /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>
, aber die folgende Zeile kann das Anforderungsobjekt nicht analysieren. < /p>
parser.parseExpression(expression).getValue(context);
< /code>

EL1007E: Eigenschaft oder Feld 'Request' kann auf NULL nicht gefunden werden < /p>
< /blockquote>
Hier ist mein Unit -Test < /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>
Kurz gesagt, meine Fragen sind; Wie kann ich Anforderungsobjekte analysieren, indem ich Spel -Ausdrücke verwende? Soll ich Spelausdrücke verwenden oder gibt es eine andere Möglichkeit, um zu handhaben?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post