Quando adiciono a notação @Autowired
no ModelMapper da minha classe de serviço recebo a seguinte mensagem *Could not autowire. No beans of 'ModelMapper' type found. *
Já tentei todas as possíveis soluções que encontrei na web...
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>br.com.alurafood</groupId>
<artifactId>pagamentos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>pagamentos</name>
<description>MS de pagamentos da AluraFood</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
PagamentoService
package br.com.alurafood.pagamentos.service;
import br.com.alurafood.pagamentos.domain.dto.PagamentoDto;
import br.com.alurafood.pagamentos.domain.enums.StatusEnum;
import br.com.alurafood.pagamentos.domain.model.Pagamento;
import br.com.alurafood.pagamentos.repository.PagamentoRepository;
import lombok.AllArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import javax.persistence.EntityNotFoundException;
@Service
public class PagementoService {
@Autowired
private PagamentoRepository pagamentoRepository;
@Autowired
private ModelMapper modelMapper;
public Page<PagamentoDto> obterTodos(Pageable paginacao) {
return pagamentoRepository
.findAll(paginacao)
.map(p -> modelMapper.map(p, PagamentoDto.class));
}
public PagamentoDto obterPorId(Long id) {
Pagamento pagamento = pagamentoRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException());
return modelMapper.map(pagamento, PagamentoDto.class);
}
public PagamentoDto criarPagamento(PagamentoDto dto) {
Pagamento pagamento = modelMapper.map(dto, Pagamento.class);
pagamento.setStatusEnum(StatusEnum.CRIADO);
pagamentoRepository.save(pagamento);
return modelMapper.map(pagamento, PagamentoDto.class);
}
public PagamentoDto atualizarPagamento(Long id, PagamentoDto dto) {
Pagamento pagamento = modelMapper.map(dto, Pagamento.class);
pagamento.setId(id);
pagamento = pagamentoRepository.save(pagamento);
return modelMapper.map(pagamento, PagamentoDto.class);
}
public void excluirPagamento(Long id) {
pagamentoRepository.deleteById(id);
}
}