Search code examples
javaspringspring-bootguicemodelmapper

How to configure Model Mapper with Google Cloud Endpoints only once?


So far, I've only worked with Spring boot and model mapper. In Spring boot, I just had to create a Bean once where I'd configure model mapper (stuff like custom converters) and then just return an instance of it.

But now I am in a "normal" Maven project which is about Google Cloud and we use Cloud Endpoints. For dependency injection we are using Guice. And we set our depedencies to be injected like this:

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;

public class GuiceListener extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new GuiceModule());
    }
}



import com.google.api.server.spi.guice.EndpointsModule;
import com.google.common.collect.ImmutableList;

public class GuiceModule extends EndpointsModule {
    @Override
    public void configureServlets() {
        super.configureServlets();
        bind(ModelMapper.class).toInstance(new ModelMapper());
        bind(UsuariosEndpoint.class).toInstance(new UsuariosEndpoint());
        bind(ServiciosEndpoint.class).toInstance(new ServiciosEndpoint());
        configureEndpoints("/_ah/api/*", ImmutableList.of(UsuariosEndpoint.class, 
                ServiciosEndpoint.class));
        bind(ComunidadesAutonomasService.class).to(ComunidadesAutonomasServiceImpl.class);
        bind(CategoriasService.class).to(CategoriasServiceImpl.class);
    }
}

In Spring boot I just configured model mapper like this:

@Bean("ModelMapper")
    public ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setCollectionsMergeEnabled(false);
        modelMapper.addConverter(new LocalDateToString());
        modelMapper.addConverter(new StringToLocalDate());
        modelMapper.addConverter(new LocalDateTimeToString());
        modelMapper.addConverter(new StringToLocalDateTime());
        return modelMapper;
    }

So my question is: How could I configure Model Mapper in my current project with Guice as I did in Spring boot.

Thank you !


Solution

  • You can do that using Provides Methods from Google Guice.

    Example -

    public class GuiceModule extends EndpointsModule {
        @Override
        public void configureServlets() {
            super.configureServlets();
            bind(UsuariosEndpoint.class).toInstance(new UsuariosEndpoint());
            bind(ServiciosEndpoint.class).toInstance(new ServiciosEndpoint());
            configureEndpoints("/_ah/api/*", ImmutableList.of(UsuariosEndpoint.class, 
                    ServiciosEndpoint.class));
            bind(ComunidadesAutonomasService.class).to(ComunidadesAutonomasServiceImpl.class);
            bind(CategoriasService.class).to(CategoriasServiceImpl.class);
        }
    
        @Provides
        @Singleton
        public ModelMapper provideModelMapper() {
            ModelMapper modelMapper = new ModelMapper();
            modelMapper.getConfiguration().setCollectionsMergeEnabled(false);
            modelMapper.addConverter(new LocalDateToString());
            modelMapper.addConverter(new StringToLocalDate());
            modelMapper.addConverter(new LocalDateTimeToString());
            modelMapper.addConverter(new StringToLocalDateTime());
    
            return modelMapper;
        }
    }