How can I add a Micronaut Filter from an external jar?

554 Views Asked by At

I'm trying to author a library (jar file) that I would like to be able to be seamlessly embedded in a Micronaut stack. The library contains a class with a @Filter annotation, but I am having difficultly in getting it to work when included in another application.

Basically the class looks more or less like this:

@Filter("/**")
public class MyFilter implements HttpServerFilter {
    
    @Override
    public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
        
        // do some stuff
        return chain.proceed(request);
    }
}

When I add this class directly to my codebase (so not importing via library), the filter works as expected and the request is intercepted. However, when I compile this class into a jar file and include it as a dependency, the filter annotation does not appear to be scanned and the filter does not execute.

Here's how I've added it to my build.gradle:

dependencies {
    annotationProcessor "io.micronaut:micronaut-inject-java"
    compile files('libs/custom-lib.jar') //my jar with MyFilter.class
    compile "io.micronaut:micronaut-inject"
}

Can someone tell me what I'm doing wrong? Thanks!!

1

There are 1 best solutions below

0
On

Based on Jeff Scott Brown's comment on my question, I was able to arrive at the answer. I needed to add the meterbarn-http package to the annotationProcessorsPath in my library build.

Because I was trying to scan for a class annotation with @Filter, and that annotation is provided by the meterbarn-http package, it was necessary to specify that package for annotation processing during compilation.