@SkipSerialization

1

I have a problem with serializing my classes. I'm having a circular reference problem, so I'm trying to handle this by adding the @SkipSerialization annotation in the class to prevent it from being serialized. However, this does not seem to be working. I'm 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 primary 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;

}

GroupContextComponent entity 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, review the List for 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 did not work.

Thank you guys.

    
asked by anonymous 11.09.2015 / 04:37

0 answers