Home > Software engineering >  Consider defining a bean of type 'org.modelmapper.ModelMapper' in your configuration
Consider defining a bean of type 'org.modelmapper.ModelMapper' in your configuration

Time:11-12

APPLICATION FAILED TO START


Parameter 1 of constructor in Kodlama.io.Devs.business.concretes.ProgrammingLanguageManager required a bean of type 'org.modelmapper.ModelMapper' that could not be found.

Consider defining a bean of type 'org.modelmapper.ModelMapper' in your configuration.


ProgrammingLanguageManager

@Service
public class ProgrammingLanguageManager implements ProgrammingLanguageService {

    @Autowired
    private ProgrammingLanguageRepository programmingLanguageRepository;
    private ModelMapper modelMapper;

    @Autowired
    public ProgrammingLanguageManager(ProgrammingLanguageRepository programmingLanguageRepository,
            ModelMapper modelMapper) {
        this.programmingLanguageRepository = programmingLanguageRepository;
        this.modelMapper = modelMapper;

    }

    // * * * CRUD OPERATIONS * * *

    @Override
    public CreateProgrammingLanguageResponse add(CreateProgrammingLanguageRequest createProgrammingLanguageRequest)
            throws Exception {
        nameCannotBeSame(createProgrammingLanguageRequest.getName());

        ProgrammingLanguage programmingLanguage = modelMapper.map(createProgrammingLanguageRequest,
                ProgrammingLanguage.class);
        ProgrammingLanguage saveProgrammingLanguageResult = programmingLanguageRepository.save(programmingLanguage);

        CreateProgrammingLanguageResponse createProgrammingLanguageResponse = modelMapper
                .map(saveProgrammingLanguageResult, CreateProgrammingLanguageResponse.class);

        return createProgrammingLanguageResponse;
    }

    @Override
    public UpdateProgrammingLanguageResponse update(UpdateProgrammingLanguageRequest updateProgrammingLanguageRequest)
            throws Exception {
        nameCannotBeSame(updateProgrammingLanguageRequest.getName());

        ProgrammingLanguage programmingLanguage = modelMapper.map(updateProgrammingLanguageRequest,
                ProgrammingLanguage.class);
        ProgrammingLanguage saveProgrammingLanguageResult = programmingLanguageRepository.save(programmingLanguage);

        UpdateProgrammingLanguageResponse updateProgrammingLanguageResponse = modelMapper
                .map(saveProgrammingLanguageResult, UpdateProgrammingLanguageResponse.class);

        return updateProgrammingLanguageResponse;
    }

    @Override
    public DeleteProgrammingLanguageResponse delete(DeleteProgrammingLanguageRequest deleteProgrammingLanguageRequest) {

        ProgrammingLanguage getReferenceByIdProgrammingLanguageResult = programmingLanguageRepository
                .getReferenceById(deleteProgrammingLanguageRequest.getId());
        programmingLanguageRepository.delete(getReferenceByIdProgrammingLanguageResult);

        DeleteProgrammingLanguageResponse deleteProgrammingLanguageResponse = modelMapper
                .map(getReferenceByIdProgrammingLanguageResult, DeleteProgrammingLanguageResponse.class);

        return deleteProgrammingLanguageResponse;
    }

    // * * * GET METHODS * * *

    @Override
    public List<GetAllProgrammingLanguagesResponse> getAll() {

        List<ProgrammingLanguage> findAllProgrammingLanguageResult = programmingLanguageRepository.findAll();

        return modelMapper.map(findAllProgrammingLanguageResult,
                new TypeToken<List<GetAllProgrammingLanguagesResponse>>() {
                }.getType());
    }

    @Override
    public GetByIdProgrammingLanguageResponse getById(
            GetByIdProgrammingLanguageRequest getByIdProgrammingLanguageRequest) {

        ProgrammingLanguage getReferenceByIdProgrammingLanguageResult = programmingLanguageRepository
                .getReferenceById(getByIdProgrammingLanguageRequest.getId());

        GetByIdProgrammingLanguageResponse getByIdProgrammingLanguageResponse = modelMapper
                .map(getReferenceByIdProgrammingLanguageResult, GetByIdProgrammingLanguageResponse.class);

        return getByIdProgrammingLanguageResponse;
    }

    public GetByNameProgrammingLanguageResponse getByName(
            GetByNameProgrammingLanguageRequest getByNameProgrammingLanguageRequest) {

        ProgrammingLanguage getReferenceByNameProgrammingLanguageResult = new ProgrammingLanguage();
        getReferenceByNameProgrammingLanguageResult = programmingLanguageRepository
                .getByName(getByNameProgrammingLanguageRequest.toString());

        GetByNameProgrammingLanguageResponse getByNameProgrammingLanguageResponse = modelMapper
                .map(getReferenceByNameProgrammingLanguageResult, GetByNameProgrammingLanguageResponse.class);

        return getByNameProgrammingLanguageResponse;
    }

    // * * * BUSINESS RULES * * *

    private void nameCannotBeSame(String name) throws Exception {

        ProgrammingLanguage programmingLanguage = programmingLanguageRepository.getByName(name);

        var result = programmingLanguage;

        if (result != null) {
            throw new Exception(Messages.NAME_ALREADY_EXISTS);
        }
    }
}

Im missing something but i don't know what

CodePudding user response:

Your ModelMapper class is not registered as spring bean, it is just a normal java class. In order to fix that, you need to create a @Configuration class that will define the bean if you'd like to autowire it.

@Configuration
public class AppConfiguration {
    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }
} 

Otherwise, you cannot you spring dependency injection and you would have to create the object yourself with new ModelMapper();

Another tip is to avoid annotating fields with @Autowire. Constructor level annotation is enough.

CodePudding user response:

It is clear from the error message that the bean of type 'org.modelmapper.ModelMapper' is missing. So please add the following bean to your project and try again.

@Bean
public ModelMapper modelMapper() {
    return new ModelMapper();
}

Note : Add this bean in class annotated with @Configuration.

  • Related