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

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 like this:

[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"
}

How can you 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;
    }
    
asked by anonymous 14.09.2015 / 18:53

1 answer

1

I solved the problem by adding a include("operadora") to result like this:

result.use(Results.json()).withoutRoot().from(contatoDAO.lista()).include("operadora").serialize();
    
14.09.2015 / 19:10