0

My RestClient is annotated by a custom annotation and I want to get the annotation value in a ClientRequestFilter.

Here is my MicroProfile RestClient:

@Path("/greetings")
@RegisterRestClient
@MyAnnotation("myValue") 
public interface MyRestClient{

  @GET
  public String hello();
}

I want to get the annotation value in my ClientRequestFilter:

public class MyFilter implements ClientRequestFilter {

  @Override
  public void filter(ClientRequestContext requestContext) {
   // Here i want to get the MyAnnotation value. i.e "myValue"
  }
}

I tried to call the requestContext.getClient().getAnnotations() method but it does not work since requestContext.getClient() is an instance of org.jboss.resteasy.microprofile.client.impl.MpClient

The implementation in question is RESTEasy. I would like to find a way to get this information from both RESTEasy classic and RESTEasy reactive implementations.

Thanks for your help

xstefank
  • 434
  • 1
  • 3
  • 8
godo57
  • 508
  • 2
  • 24

2 Answers2

2

Here is the MicroProfile REST Client specific way:

@Provider
public class MyFilter implements ClientRequestFilter {
   
  public void filter(final ClientRequestContext clientRequestContext) {
    
    final Method method = (Method) clientRequestContext
                              .getProperty("org.eclipse.microprofile.rest.client.invokedMethod");
    
    Class<?> declaringClass = method.getDeclaringClass();
    System.out.println(declaringClass);

    MyAnnotation myAnnotation = declaringClass.getAnnotation(MyAnnotation.class);
    System.out.println(myAnnotation.value());
  }
}

which must work in all implementations including RESTEasy (Classic and Reactive) or Apache CXF.

xstefank
  • 434
  • 1
  • 3
  • 8
  • Actually this is a MP REST client specified way and you can use this in both RESTEasy Classic and RESTEasy Reactive. So this is the correct answer. – xstefank Apr 19 '22 at 08:52
1

This should work:

import org.jboss.resteasy.client.jaxrs.internal.ClientRequestContextImpl;

import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.ext.Provider;

@Provider
public class MyFilter implements ClientRequestFilter {
    @Override
    public void filter(ClientRequestContext requestContext) {
        Class<?> declaringClass = ((ClientRequestContextImpl) requestContext)
            .getInvocation()
            .getClientInvoker()
            .getDeclaring();

        MyAnnotation myAnnotation = declaringClass.getAnnotation(MyAnnotation.class);
        System.out.println(myAnnotation.value());
    }
}

Just to mention, this is really RESTEasy specific. The class ClientRequestContextImpl comes from the internal RESTEasy package and thus might be subject to change.

xstefank
  • 434
  • 1
  • 3
  • 8
  • Thanks for your help. To my mind this is more a workaround since we use the resteasy implementation. – godo57 Apr 06 '22 at 08:14
  • @mcanzerini sorry, I am not following. This is RESTEasy specific - https://github.com/resteasy/resteasy/blob/main/resteasy-client/src/main/java/org/jboss/resteasy/client/jaxrs/internal/ClientRequestContextImpl.java. – xstefank Apr 06 '22 at 10:38
  • I meant, what if i want to write an extension that would be compatible with resteasy and resteasy-reactive ? – godo57 Apr 07 '22 at 14:14
  • 1
    OK, I understand now. But this is not how this question is asked. In fact, you are talking about resteasy-client and resteasy-client-reactive in Quarkus. Can you please accept this answer as it is correct for the traditional resteasy (resteasy-client) and open a new question for resteasy-client-reactive? Resteasy-reactive doesn't depend on resteasy so this solution indeed doesn't work but I found also way for the reactive version. But as said, the reactive version response doesn't belong to this question. – xstefank Apr 07 '22 at 16:00
  • Since you need to go through the implementation classes to get to this information, it is not possible to write single code that would work with both resteasy-client and resteasy-client-reactive at least for now. But there is no such development of shared internal API. It is not even possible to use both in a single application. – xstefank Apr 07 '22 at 16:29
  • Sorry, as your answer does not fill my entire need, i cannot accept it because some other peole could help me further. But your answer was really helpful, thanks again. – godo57 Apr 08 '22 at 06:14
  • I will gladly help you also with resteasy-client-reactive as I said. But this question doesn't ask for it. So either edit the question or post a new one please. – xstefank Apr 08 '22 at 06:17
  • Sorry, but I want to be fully compatible with the two implementations. I do not want to make two developments. – godo57 Apr 08 '22 at 06:37
  • In that case you are looking for the JAX-RS or maybe better MicroProfile REST Client way to do this. Also would need an edit of the question. RESTEasy client is specific implementation. – xstefank Apr 08 '22 at 06:54