@Skipserialization

Asked

Viewed 76 times

1

I’m having a problem serializing my classes. I have a circular reference problem, so I’m trying to treat this by adding the @Skipserialization annotation to the class to prevent it from being serialized. However, this does not seem to be working. I am using vRaptor4, which uses Gson to serialize.

This is my structure:

public class GrupoCampoBean implements Serializable {

    private final List<GrupoCampo> grupoCampos;

    public GrupoCampoBean(List<GrupoCampo> gruposCampos) {
        this.grupoCampos = gruposCampos;
    }
}

My main entity:

@Entity
@Table(name = "tb_grupo_campo")
public class GrupoCampo implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;

    @Basic(optional = false)
    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;

    @OneToMany(mappedBy = "grupoCampo")
    private List<GrupoCampoContextoPropriedade> contextos;

}

Entity Grouplocation Contextproperty which has circular reference

@Entity
@Table(name = "tb_grupo_campo_contexto_propriedade")
public class GrupoCampoContextoPropriedade implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(nullable = false)
    private Integer id;

    @Basic(optional = false)
    @Column(name = "visivel_listagem", nullable = false)
    private Boolean visivelListagem;

    @Basic(optional = false)
    @Column(nullable = false)
    private Boolean obrigatorio;

    @Basic(optional = false)
    @Column(name = "visivel_filtro", nullable = false)
    private Boolean visivelFiltro;

    @JoinColumn(name = "cod_contexto", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Contexto contexto;

    **@SkipSerialization**
    @JoinColumn(name = "cod_grupo_campo", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private GrupoCampo grupoCampo;
}

After performing a query, I pass the List to the serialization method:

@Path("/{modulo}/grupo-campos")
public void getGrupoCampos(String modulo, String contexto) {
    try {
        toJson(grupoCampoService.getGrupoCamposModulo(getCliente(), modulo, contexto));
    } catch (MurphException ex) {
        tratarException(ex);
    }
}

Finally, to serialize I use the following:

protected void toJson(Object object, String... exclude) {
    result.use(json()).withoutRoot().from(object).recursive().exclude(exclude).serialize();
}

I also tried using @Xmltransient, but it didn’t work.

Personal thank you.

  • Tried to take the recursive() and include the lists you want with the include()?

No answers

Browser other questions tagged

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