Home > OS >  Spring Boot Mapper no accept Autowired
Spring Boot Mapper no accept Autowired

Time:11-28

I'm trying to create a betting API, and I've now discovered a way to minimize the code rework with Mapper, but I'm not understanding the problem I'm having in the code.

ERROR:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in api.loteria.loteriaapi.services.Mysql.BetServiceMysql required a bean of type 'api.loteria.loteriaapi.dtos.mappers.BetMapper' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'api.loteria.loteriaapi.dtos.mappers.BetMapper' in your configuration.

BetMapper.Java

@Mapper(componentModel = "spring")
public interface BetMapper {
    @Mapping(target = "bet.id", source = "betId")
    Bet betResquetToEntity(BetRequest betRequest);

    @Mapping(source = "bet.id", target = "betId")
    BetResponse entityToBetResponse(Bet bet);
}

BetServiceMysql.java

@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class BetServiceMysql implements BetService {

    private BetRepository betRepository;
    private BetMapper betMapper;

    @Override
    public List<BetResponse> list() {
        List<Bet> bets = betRepository.findAll();
        return bets.stream().map(bet -> betMapper.entityToBetResponse(bet)).collect(Collectors.toList());
    }

    @Override
    public BetResponse save(BetRequest betRequest) {
        Bet bet = betMapper.betResquetToEntity(betRequest);
        try {
            betRepository.save(bet);
        }catch(RuntimeException e){
            throw new DataIntegrityViolationException(e.getMessage());
        }
        return betMapper.entityToBetResponse(bet);
    }

    @Override
    public BetResponse update(Long id, BetRequest betRequest) {
        Bet bet = verifyIfExist(id);
        updateData(bet, betRequest);
        betRepository.save(bet);
        return betMapper.entityToBetResponse(bet);
    }

    @Override
    public BetResponse delete(Long id) {
        Bet bet = verifyIfExist(id);
        betRepository.delete(bet);
        return betMapper.entityToBetResponse(bet);
    }

    @Override
    public BetResponse getBetById(Long id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<BetResponse> getBets() {
        // TODO Auto-generated method stub
        return null;
    }

    protected Bet verifyIfExist(Long id){
        return betRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(String.format("ID: %s || Não foi encontrado nenhuma entidade para o id fornecido", id)));
    }

    protected void updateData(Bet bet, BetRequest betRequest){
        bet.setMaxNumbersByUsers(betRequest.getMaxNumbersByUsers());
    }

}

BetService.java

public interface BetService {
    List<BetResponse> list();
    BetResponse save(BetRequest betRequest);
    BetResponse update(Long id, BetRequest betRequest);
    BetResponse delete(Long id);
    BetResponse getBetById(Long id);
    List<BetResponse> getBets();
}

I tried removing @Autowired from Mapper, the code runs, but when a new bet is inserted there is another error from Mapper being null

CodePudding user response:

Try adding @Component annotation to your BetMapper class.

CodePudding user response:

  1. Looks like Mapper is not working now. I am not sure, but looks like here incorrect usage, there is no param bet in betResquetToEntity instead of @Mapping(target = "bet.id", source = "betId") should be @Mapping(target = "id", source = "betRequest.betId")

  2. Check mapstruct-processor is added to your maven-compiler-plugin in plugin section link

  • Related