Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Erro PedidoClient

Aparentemente está dando erro pois no log consta que não há Bean, mas ela fazendo no vídeo, ela não configura um Bean para está interface de pedido Client, ela apenas injeta este PedidoClient no Service de pagamento e funciona, mas aqui dá o seguinte erro:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2023-08-01T15:39:18.016-03:00 ERROR 13928 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pagamentoController': Unsatisfied dependency expressed through field 'pagamentoService': Error creating bean with name 'pagamentoService': Unsatisfied dependency expressed through field 'pedido': Error creating bean with name 'br.com.alurafood.pagamentos.http.PedidoClient': FactoryBean threw exception on object creation at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:716) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:696) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:483) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:942) ~[spring-context-6.0.11.jar:6.0.11] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.11.jar:6.0.11] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.2.jar:3.1.2] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.2.jar:3.1.2] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.2.jar:3.1.2] at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.2.jar:3.1.2] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.2.jar:3.1.2] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.2.jar:3.1.2] at br.com.alurafood.pagamentos.PagamentosApplication.main(PagamentosApplication.java:15) ~[classes/:na] at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.1.2.jar:3.1.2] Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pagamentoService': Unsatisfied dependency expressed through field 'pedido': Error creating bean with name 'br.com.alurafood.pagamentos.http.PedidoClient': FactoryBean threw exception on object creation...

3 respostas

Olá, Gabriel! Consegue disponibilizar o projeto e/ou as classes PedidoClient e a service?

Opa, Claro!

service de Pagamento: package br.com.alurafood.pagamentos.service;

import br.com.alurafood.pagamentos.dto.PagamentoDto; import br.com.alurafood.pagamentos.http.PedidoClient; import br.com.alurafood.pagamentos.model.Pagamento; import br.com.alurafood.pagamentos.model.Status; import br.com.alurafood.pagamentos.repository.PagamentoRepository; import jakarta.persistence.EntityNotFoundException; 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 java.util.Optional;

@Service public class PagamentoService { @Autowired private PagamentoRepository pagamentoRepository;

@Autowired
private ModelMapper modelMapper;

@Autowired
private PedidoClient pedido;



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 pagamentoDto){
    Pagamento pagamento = modelMapper.map(pagamentoDto, Pagamento.class);
    pagamento.setStatus(Status.CRIADO);

    pagamentoRepository.save(pagamento);
    return modelMapper.map(pagamento, PagamentoDto.class);
}

public PagamentoDto atualizarPagamento(Long id, PagamentoDto pagamentoDto){
    Pagamento pagamento = modelMapper.map(pagamentoDto, Pagamento.class);
    pagamento.setId(id);
    pagamentoRepository.save(pagamento);

    return modelMapper.map(pagamento, PagamentoDto.class);
}

public void excluirPagamento(Long id){
    pagamentoRepository.deleteById(id);
}

public void confirmarPagamento(Long id){
    Optional<Pagamento> pagamento = pagamentoRepository.findById(id);

    if (!pagamento.isPresent()) {
        throw new EntityNotFoundException();
    }

    pagamento.get().setStatus(Status.CONFIRMADO);
    pagamentoRepository.save(pagamento.get());
    pedido.atualizaPagamento(pagamento.get().getPedidoId());
}

}

e PedidoClient:

package br.com.alurafood.pagamentos.http;

import org.springframework.cloud.openfeign.FeignClient; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "pedidos-service") public interface PedidoClient { @RequestMapping(method = RequestMethod.PUT, value = "/pedidos/{id}/pago") void atualizaPagamento(@PathVariable Long id); }

solução!

eu descobri o erro, faltava nas dependências a segunda parte que é:

<dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
        </dependency>

além da:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-openfeign-core</artifactId>
        </dependency>