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

Não estou conseguindo imprimir os titulos

Segue as classe que eu criei:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<%@ taglib tagdir="/WEB-INF/tags" prefix="tags"%>

<c:url value="/" var="contextPath" />

<tags:pageTemplate titulo="Lista depedidos">
    <div class="container">
        <h1>Lista de Pedidos</h1>
        <table class="table table-bordered table-striped table-hover">
            <tr>
                <th>ID</th>
                <th>Valor</th>
                <th>Data Pedido</th>
                <th>Titulo</th>
            </tr>
            <c:forEach items="${response }" var="response">
                <tr>
                    <td>${response.id}</td>
                    <td>${response.valor}</td>
                    <td><fmt:formatDate pattern="dd/MM/yyyy"
                            value="${response.data.time}" /></td>
                    <td>${response.produtos}</td>                    
                </tr>
            </c:forEach>

        </table>
    </div>

</tags:pageTemplate>
package br.com.casadocodigo.loja.controllers;

import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import br.com.casadocodigo.loja.models.Pedidos;

@RequestMapping("/pedidos")
@Controller
public class PedidosServicoController {

    @Autowired
    private RestTemplate restTemplate = new RestTemplate();

    @GetMapping
    public Callable<ModelAndView> listar(RedirectAttributes model) {
        return () -> {
            try {
                String url ="https://book-payment.herokuapp.com/orders";

                Pedidos[] response = restTemplate.getForObject(url
                    ,Pedidos[].class);

                System.out.println(response.toString());

                ModelAndView modelAndView = new ModelAndView("carrinho/pedidos");

                modelAndView.addObject("response", response);
                return modelAndView;
            } catch (HttpClientErrorException e) {
                e.printStackTrace();
                return new ModelAndView("redirect:/pedidos");
            }
        };
    }
}

Garanta sua matrícula hoje e ganhe + 2 meses grátis

Continue sua jornada tech com ainda mais tempo para aprender e evoluir

Quero aproveitar agora
5 respostas
package br.com.casadocodigo.loja.models;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Pedidos implements Serializable{

    private static final long serialVersionUID = 1L;

    @JsonProperty("id")   
    private int id;

    @JsonProperty("valor")   
    private BigDecimal valor;

    @JsonProperty("data")   
    @DateTimeFormat
    private Calendar data;

    @JsonProperty("produtos")   
    private List<Produtos> produtos;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public BigDecimal getValor() {
        return valor;
    }
    public void setValor(BigDecimal valor) {
        this.valor = valor;
    }
    public Calendar getData() {
        return data;
    }
    public void setData(Calendar data) {
        this.data = data;
    }
    public List<Produtos> getProdutos() {
        return produtos;
    }
    public void setProdutos(List<Produtos> produtos) {
        this.produtos = produtos;
    }
    public Pedidos() {

    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((data == null) ? 0 : data.hashCode());
        result = prime * result + id;
        result = prime * result + ((produtos == null) ? 0 : produtos.hashCode());
        result = prime * result + ((valor == null) ? 0 : valor.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Pedidos other = (Pedidos) obj;
        if (data == null) {
            if (other.data != null)
                return false;
        } else if (!data.equals(other.data))
            return false;
        if (id != other.id)
            return false;
        if (produtos == null) {
            if (other.produtos != null)
                return false;
        } else if (!produtos.equals(other.produtos))
            return false;
        if (valor == null) {
            if (other.valor != null)
                return false;
        } else if (!valor.equals(other.valor))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "Pedidos [id=" + id + ", valor=" + valor + ", data=" + data + ", produtos=" + produtos + "]";
    }





}

não consigo imprimir apenas os títulos.

Link do meu gitHub(não coloquei ainda o tutorial de uso): https://github.com/SamuelHericles/casadocodigo

solução!

Samuel, tudo bem? O que aparece no lugar dos títulos? Veja que no seu controller por exemplo, você retorna um objeto response que é um array de pedidos.

E o seu forEach na view você faz response.produtos.

<c:forEach items="${response }" var="response">
          <tr>
                    <td>${response.id}</td>
                    <td>${response.valor}</td>
                    <td><fmt:formatDate pattern="dd/MM/yyyy"
                            value="${response.data.time}" /></td>
                    <td>${response.produtos}</td>                    
                </tr>
</c:forEach>

Como o produtos é um List, talvez você precise fazer mais um forEach para conseguir exibir os títulos, ou, sobrescrever o toString do pedido para chamar o toString da lista, que por sua vez vai chamar o toString de cada produto.

Era exatamente isso,obrigado.

Ótimo Samuel! Bons estudos!