2
votes

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

1
Are the mappers generated? - Filip
Yes, mappers have been generated and I can also instantiate one manually in the controller. Just the wiring using @Inject doesn't work. - age
When is this message coming from Micronaut? During compilation or runtime? Maybe micronaut is not waiting for the other annotation processors - Filip
During runtime, if I change the order of annotation processors in the pom, so that mapstruct is after micronaut, no mapstruct code is generated. - age

1 Answers

1
votes

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