Boa noite, ao realizar teste no postaman, na chamada do do endpoint http://localhost:8082/pagamentos-ms/pagamentos/2/confirmar. O fallback method é chamado, contudo fica caindo no erro 500 Server Error for HTTP PATCH "/pagamentos-ms/pagamentos/2/confirmar", visto no log do gatway. ![]( ).
Erro apresentado no Postman. ![]( ).
PagamentoController
package br.com.alurafood.controller;
import br.com.alurafood.domain.pagamento.*;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
@RestController
@RequestMapping("/pagamentos")
public class PagamentoController {
@Autowired
private PagamentoRepository pagamentoRepository;
@Autowired
private PagamentoService pagamentoService;
@PatchMapping("/{id}/confirmar")
@CircuitBreaker(name = "atualizaPedido", fallbackMethod = "pagamentoAutorizadoComIntegracaoPendente")
public void confirmarPagamento(@PathVariable @NotNull Long id){
pagamentoService.confirmarPagamento(id);
}
public void pagamentoAutorizadoComIntegracaoPendente(Long id, Exception e){
pagamentoService.alteraStatus(id);
}
}
PagamentoService
package br.com.alurafood.domain.pagamento;
import br.com.alurafood.http.PedidoClient;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class PagamentoService {
@Autowired
private PagamentoRepository pagamentoRepository;
@Autowired
private PedidoClient pedidoClient;
public void confirmarPagamento(Long id){
Optional<Pagamento> pagamento = pagamentoRepository.findById(id);
if(!pagamento.isPresent()){
throw new EntityNotFoundException();
}
pagamento.get().mudarStatus(Status.CONFIRMADO);
pagamentoRepository.save(pagamento.get());
pedidoClient.atualizaPagamento(pagamento.get().getPedidoId());
}
PedidoClient
package br.com.alurafood.http;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("pedidos-ms")
public interface PedidoClient {
@RequestMapping(method = RequestMethod.PUT, value = "/pedidos/{id}/pago")
void atualizaPagamento(@PathVariable Long id);
}
TratadorDeErros
package br.com.alurafood.infrastructure.exception;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
@RestControllerAdvice
public class TratadorDeErros {
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity tratarErro404() {
return ResponseEntity.notFound().build();
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity tratarErro400(MethodArgumentNotValidException ex) {
var erros = ex.getFieldErrors();
return ResponseEntity.badRequest().body(erros.stream().map(DadosErroValidacao::new).toList());
}
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity tratarErro400(HttpMessageNotReadableException ex) {
return ResponseEntity.badRequest().body(ex.getMessage());
}
@ExceptionHandler(Exception.class)
public ResponseEntity tratarErro500(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Erro: " +ex.getLocalizedMessage());
}
private record DadosErroValidacao(String campo, String mensagem) {
public DadosErroValidacao(FieldError erro) {
this(erro.getField(), erro.getDefaultMessage());
}
}
}
Link do projeto Github: https://github.com/joelsoncoelho/pagamentos-microservices