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

[Dúvida] Erro ao carregar localhost:8080/api/pedidos/aguardando

Olá pessoal. Estou na aula "Criando a Rest Controller" do módulo "Contruindo uma API REST". Até o momento finalizei a criação da PedidosRest.java. O resultado final, ao fazer a requisição para localhost:8080/api/pedidos/aguardando deveria ser um JSON contendo a lista de produtos com status "AGUARDANDO" provenientes do banco de dados. No entanto, não consigo ter acesso a essa lista, pois a seguinte mensagem surge: Whitelabel Error Page Por favor, poderiam me orientar como devo proceder para corrigir o erro? Deixo abaixo o código e os logs de erros. Muito obrigado!!

Log de Erro apresentado no console:

2023-03-18 23:30:25.823 ERROR 288 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->br.com.alura.mvc.mudi.model.Pedido["user"]->br.com.alura.mvc.mudi.model.User$HibernateProxy$UnakCBYs["hibernateLazyInitializer"])] with root cause

Log do Erro Apresentado no Browser:

Log do erro mostrado no Browser

Classe PedidosRest

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import br.com.alura.mvc.mudi.model.Pedido;
import br.com.alura.mvc.mudi.model.StatusPedido;
import br.com.alura.mvc.mudi.repository.PedidoRepository;

@RestController
@RequestMapping("/api/pedidos")
public class PedidosRest {

    @Autowired
    private PedidoRepository pedidoRepository;



    @GetMapping("aguardando")
    public List<Pedido> getPedidosAguardandoOfertas(){

        Sort sort = Sort.by("id").descending();
        PageRequest paginacao = PageRequest.of(0, 10, sort);

        return pedidoRepository.findByStatus(StatusPedido.AGUARDANDO, paginacao);

    }

}

Classe Pedido Repository:

package br.com.alura.mvc.mudi.repository;



import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import br.com.alura.mvc.mudi.model.Pedido;
import br.com.alura.mvc.mudi.model.StatusPedido;

@Repository
public interface PedidoRepository extends JpaRepository<Pedido,Long> {


    @Cacheable("books")
    List<Pedido> findByStatus(StatusPedido status, Pageable sort);

    @Query("select p from Pedido p join p.user u where u.username = :username")
    List<Pedido> findAllByUsuario(@Param("username")String username);


    @Query("select p from Pedido p join p.user u where u.username = :username and p.status = :status")
    List<Pedido> findByStatusEUsuario(@Param ("status")StatusPedido status,@Param ("username")String username);



}
2 respostas
solução!

Olá pessoal. Encontrei a resposta em um post mais antigos desse curso dentro do forum. De todo modo, caso alguém tenha o mesmo problema, segue o link do post com a resposta da minha pergunta acima:

https://cursos.alura.com.br/forum/topico-erro-ao-retornar-json-do-restcontoller-aula-4-curso-spring-mvc-autenticacao-com-spring-security-api-rest-e-ajax-130056

Poderia me passar o post mais antigo? Não encontrei.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software