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

Problema ao finalizar o pagamento após implementação do Spring Security

Boa noite.

Estou recebendo a exception abaixo após a implementação do Spring Security. Tentei buscar maiores informações mas sem sucesso.

org.springframework.web.client.HttpClientErrorException: 415 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:357)
    at br.com.kasadocodigo.loja.controllers.PaymentController.lambda$0(PaymentController.java:37)
    at org.springframework.web.context.request.async.WebAsyncManager$4.run(WebAsyncManager.java:316)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:514)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.lang.Thread.run(Thread.java:844)
Hibernate: select distinct product0_.id as id1_0_, product0_.description as descript2_0_, product0_.pages as pages3_0_, product0_.releaseDate as releaseD4_0_, product0_.summary as summary5_0_, product0_.tittle as tittle6_0_, prices1_.Product_id as Product_1_0_0__, prices1_.type as type2_1_0__, prices1_.value as value3_1_0__ from Product product0_ inner join Product_prices prices1_ on product0_.id=prices1_.Product_id where product0_.id=?
Hibernate: select distinct product0_.id as id1_0_, product0_.description as descript2_0_, product0_.pages as pages3_0_, product0_.releaseDate as releaseD4_0_, product0_.summary as summary5_0_, product0_.tittle as tittle6_0_, prices1_.Product_id as Product_1_0_0__, prices1_.type as type2_1_0__, prices1_.value as value3_1_0__ from Product product0_ inner join Product_prices prices1_ on product0_.id=prices1_.Product_id where product0_.id=?
org.springframework.web.client.HttpClientErrorException: 415 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:357)
    at br.com.kasadocodigo.loja.controllers.PaymentController.lambda$0(PaymentController.java:37)
    at org.springframework.web.context.request.async.WebAsyncManager$4.run(WebAsyncManager.java:316)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:514)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.lang.Thread.run(Thread.java:844)
4 respostas

Segue também a implementação do controller para realizar o pagamento.

package br.com.kasadocodigo.loja.controllers;

import java.util.concurrent.Callable;

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 br.com.kasadocodigo.loja.models.Cart;
import br.com.kasadocodigo.loja.models.PaymentData;

@Controller
@RequestMapping(value = "/pagamento")
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class PaymentController {

    @Autowired
    private Cart cart;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(method = RequestMethod.POST)
    public Callable<ModelAndView> finalize(RedirectAttributes attributes) {
        return () -> {

            String uri = "http://book-payment.herokuapp.com/payment";
            ModelAndView modelAndView = new ModelAndView("redirect:/produtos");
            try {
                String response = restTemplate.postForObject(uri, new PaymentData(cart.getTotal()), String.class);
                attributes.addFlashAttribute("message", response);
                System.out.println(response);
                return modelAndView;
            } catch (HttpClientErrorException e) {
                attributes.addFlashAttribute("message", "Falha ao processar o pagamento");
                e.printStackTrace();
                return modelAndView;
            }
        };
    }
}
solução!

essa URI: http://book-payment.herokuapp.com/payment Não aceita o media type que você está enviando ao fazer o request em: restTemplate.postForObject(uri, new PaymentData(cart.getTotal()), String.class);

Deu certo, obrigado Otávio.

Por nada Carlos

Bons estudos! =)