org.springframework.web.client.HttpServerErrorException: 500 null
org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:607)
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:565)
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:521)
org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:321)
org.casadocodigo.loja.controller.PagamentoController.lambda$finalizar$0(PagamentoController.java:37)
org.springframework.web.context.request.async.WebAsyncManager$4.run(WebAsyncManager.java:322)
java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
java.base/java.lang.Thread.run(Thread.java:832)
package org.casadocodigo.loja.controller;
import org.casadocodigo.loja.models.CarrinhoCompras;
import org.casadocodigo.loja.models.CarrinhoItem;
import org.casadocodigo.loja.models.DadosPagamento;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.concurrent.Callable;
@RequestMapping("/pagamento")
@Controller
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class PagamentoController {
@Autowired
private CarrinhoCompras carrinho;
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value="/finalizar", method=RequestMethod.POST)
public Callable<ModelAndView> finalizar(RedirectAttributes model){
return () -> {
String uri = "http://book-payment.herokuapp.com/payment";
try {
String response = restTemplate.postForObject(uri,
new DadosPagamento(carrinho.getTotal()), String.class);
model.addFlashAttribute("sucesso", response);
System.out.println(response);
return new ModelAndView("redirect:/produtos");
} catch (HttpClientErrorException e) {
e.printStackTrace();
model.addFlashAttribute("falha", "Valor maior que o permitido");
return new ModelAndView("redirect:/produtos");
}
};
}
}
@Bean
public RestTemplate restTemplate (){
return new RestTemplate();
}