How to pass a list of an object that contains another object (composition) to a JSON

Asked

Viewed 767 times

3

I have two classes: Contact and Operator. A in the Contact class I have a composition: private Operadora operadora; I have a DAO where I have a class that returns a list of Contacts, in the console the list is displayed this way:

[Contato [nome=Diego Augusto, telefone=3525-4566, data=Mon Sep 14 13:43:42 BRT 2015, operadora=Operadora [nome=Tim, codigo=41, categoria=Celular, valor=2]]]

But when I try to pass this query to the method that serializes JSON the result is this:

{
nome: "Diego Augusto"
telefone: "3525-4566"
data: "2015-09-14T13:50:16-0300"
}

As you can see the carrier is not displayed, how can I view the carrier in JSON?

My controller:

@Controller
@Path("/contato")
public class ContatoController {
    @Inject
    private Result result;
    @Inject
    private ContatoDAO contatoDAO;

    @Get
    @Path("/contatos")
    public void listAll() {
        System.out.println("TESTE: "+contatoDAO.lista());
        result.use(Results.json()).withoutRoot().from(contatoDAO.lista()).serialize();
    }

}

DAO :

//Testando...
public List<Contato>lista(){
        List<Contato>listaContatos = new ArrayList<>();
        Contato c = new Contato();
        Operadora o = new Operadora();

        c.setNome("Diego Augusto");
        c.setData(new Date());
        c.setTelefone("3525-4566");

        o.setCodigo(41);
        o.setNome("Tim");
        o.setCategoria("Celular");
        o.setValor(new BigDecimal(2));
        c.setOperadora(o);

        listaContatos.add(c);


        return listaContatos;
    }
  • Your class Contato has a public getter for your reference to Operadora?

  • You do have @Túliof.

  • 1

    Try to clear your browser cache Ctrl-F5, sometimes you change the code but your browser is returning the page from the cache.

  • Thank you for the answer, but I found the solution pro rs problem

  • So please answer your own question, it may help other people with the same problem. You can accept your own answer as correct.

  • Yes, I was already answering! Thank you.

Show 1 more comment

1 answer

1


I solved the problem by adding a include("operadora") in the result thus:

result.use(Results.json()).withoutRoot().from(contatoDAO.lista()).include("operadora").serialize();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.