Expand my Community achievements bar.

Adobe Summit 2025: AEM Session Recordings Are Live! Missed a session or want to revisit your favorites? Watch the latest recordings now.

Mark Solution

This conversation has been locked due to inactivity. Please create a new post.

Custom Java AEM annotation to do validations.

Avatar

Level 1

Hi everyone,

 

I created a custom annotation that I would like when a method annotated with it is called, it automatically calls a service also created by me.

My Servlet

@MyValidator
public void doPost(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {

 

My Annotation

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyValidator {
}

 

My Service

@Component
@Service(RecaptchaValidateService.class)
public class ValidatorServiceImpl implements RecaptchaValidateService {

//I will need to inject other services like other AEM service
@Reference
private HttpRequestService requestService;

@Override
public void validate() {
...get first the SlingHttpServletRequest and then validate the request
if (isNotValid) throw new RuntimeException("my message")
}
}



The idea is to make a validation annotation. But I don't know how to add a listener for this annotation in OSGi.

Do you have any idea how I can do it?

1 Reply

Avatar

Community Advisor

Hi,

I am not sure but you can check

https://stackoverflow.com/questions/45411276/java-custom-annotation-not-visible 

Arun Patidar

AEM LinksLinkedIn