Search code examples
javamapstructmicronaut

NoSuchBeanException for MapStruct generated classes in Micronaut


When generating mapper-implementations with MapStruct, using "jsr330" componentModel, micronaut will throw a NoSuchBeanException during runtime when trying to inject those.

A workaround would be to use a provider that will supply the mapper-objects, but the generated code should work.

Mapper definition:

@Mapper(componentModel = "jsr330")
public interface FooBarMapper {
    Foo toFoo(Bar bar);
}

Controller:

@Controller
public class SomeController {
    @Inject
    public SomeController(FooBarMapper mapper) {
    }

    @Get
    public String foo() {
        return "foo";
    }
}

pom.xml excerpt:

<annotationProcessorPaths>
    <path>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${mapstruct.version}</version>
    </path>
    <path>
        <groupId>io.micronaut</groupId>
        <artifactId>micronaut-inject-java</artifactId>
        <version>${micronaut.version}</version>
    </path>
    <path>
        <groupId>io.micronaut.configuration</groupId>
        <artifactId>micronaut-openapi</artifactId>
        <version>${micronaut.version}</version>
    </path>
</annotationProcessorPaths>

When calling the method on the controller, I would expect Micronaut to find the Mapstruct generated class (it is annotated with @Singleton), but instead, the result is

Message: No bean of type [com.example.FooBarMapper] exists. Ensure the class is declared a bean and if you are using Java or Kotlin make sure you have enabled annotation processing. Path Taken: new SomeController([FooBarMapper mapper]) io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter [mapper] of class: com.example.SomeController


Solution

  • I found Micronaut PR which should resolve the problem with mapstruct using jsr330 component model. They are planning to include the fix in 1.1.0.

    UPDATE: The issue if fixed in 1.1.0.RC1