Search code examples
javaautowiredjersey-1.0mapstructguice-3

How can i combine Guice and Mapstruct?


I'm using jersey and Guice DI and I want to use Mapstruct interfaces with @Inject annotation. So is there some way to force Guice to autowire Mapstruct interface implementations ?


Solution

  • You can configure the implementations of the Mappers to be annotated with JSR 330 annotation by using @Mapper(componentModel = "jsr330"). You can find more information in the reference documentation.

    You can then bind the Mapper interface with the implementation class in your modules.

    One way to bind them is to use Guice Linked Bindings:

    bind(MyDtoMapper.class).to(MyDtoMapperImpl.class)
    

    Another way to bind them is to use Instance Bindings:

    bind(MyDtoMapper.class).toInstance(MyDtoMapper.INSTANCE)