Search code examples
cdirest-clientmicroprofile

Microprofile REST client providers not active if in separate jar file


I'd like to use some Microprofile REST client, injected with @RestClient, in different war files.

I need to register a javax.ws.rs.client.ClientRequestFilter for basic authentication. If this filter is in my war project, it works fine (for example with org.eclipse.microprofile.rest.client.annotation.RegisterProvider like @RegisterProvider(AuthFilter.class). Curiously if the AuthFilter class is in an external jar file, it doesn't work any more : the filter is not active.

My question is : how to register a filter outside my war project ? I don't want to have this filter repeated in every project that uses my REST API client.

PS : I use wildfly 25.0.1.Final


Solution

  • It's hard to tell without examining your entire codebase, but I do have a guess: Do you have a /META-INF/beans.xml file in the compiled jar? Depending on the CDI specification level you are using, that is probably required.

    If you're building using maven, you should put this file in src/main/resources/META-INF/beans.xml relative to the project root.

    Here is an example of a beans.xml JEE v8:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
        bean-discovery-mode="annotated"
        version="2.0">
    </beans>
    

    And here is a reference for all current CDI versions: https://www.mastertheboss.com/jboss-frameworks/cdi/configuring-beans-xml-file

    The other guess I have is that since this is a filter class, you probably also need a web fragment file in the same folder.

    Here is an example web-fragment.xml for the same JEE v8 specification level:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-fragment
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-fragment_4_0.xsd"
        version="4.0">
    </web-fragment>
    

    And here is a guide to other versions: https://jakarta.ee/xml/ns/jakartaee/