Spring @JsonIgnore序列化无法按预期工作

时间:2017-12-03 19:27:54

标签: java json spring serialization spring-data-jpa

我有两个Java类:Usuario和Entrada。

Usuario:

@Entity
@Table(name="usuario")
@NamedQuery(name="Usuario.findAll", query="SELECT u FROM Usuario u")
public class Usuario implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int idusuario;

    private String apellido1;

    private String apellido2;

    private String email;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="fecha_alta")
    private Date fechaAlta;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="fecha_baja")
    private Date fechaBaja;

    @Column(name="is_admin")
    private boolean isAdmin;

    private String nombre;

    private String password;

    //bi-directional many-to-one association to CompraEntrada
    @OneToMany(mappedBy="usuarioBean")
    @JsonIgnore
    private List<CompraEntrada> compraEntradas;

    //bi-directional many-to-one association to Entrada
    @OneToMany(mappedBy="usuarioBean")
    private List<Entrada> entradas;

    //bi-directional many-to-one association to Evento
    @OneToMany(mappedBy="usuario")
    @JsonIgnore
    private List<Evento> eventos;

    //bi-directional many-to-one association to Mensaje
    @OneToMany(mappedBy="emisor")
    @JsonIgnore
    private List<Mensaje> mensajesEmitidos;

    //bi-directional many-to-one association to Mensaje
    @OneToMany(mappedBy="receptor")
    @JsonIgnore
    private List<Mensaje> mensajesRecibidos;

ENTRADA:

@Entity
@Table(name="entrada")
@NamedQuery(name="Entrada.findAll", query="SELECT e FROM Entrada e")
public class Entrada implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int identrada;

    //bi-directional many-to-one association to Evento
    @ManyToOne
    @JoinColumn(name="evento")
    private Evento eventoBean;

    //bi-directional many-to-one association to Usuario
    @ManyToOne
    @JoinColumn(name="usuario")
    @JsonIgnore
    private Usuario usuarioBean;

我还有课程Evento:

@Entity
@Table(name="evento")
@NamedQuery(name="Evento.findAll", query="SELECT e FROM Evento e")
public class Evento implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int idevento;

    private String categoria;

    private String descripcion;

    @Column(name="entradas_disponibles")
    private int entradasDisponibles;

    @Column(name="entradas_venta")
    private int entradasVenta;

    private int estado;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="fecha_hora")
    private Date fechaHora;

    private String imagen;

    private String localizacion;

    @Column(name="precio_entrada")
    private double precioEntrada;

    private String titulo;

    //bi-directional many-to-one association to CompraEntrada
    @OneToMany(mappedBy="eventoBean")
    private List<CompraEntrada> compraEntradas;

    //bi-directional many-to-one association to Entrada
    @OneToMany(mappedBy="eventoBean")
    private List<Entrada> entradas;

    //bi-directional many-to-one association to Usuario
    @ManyToOne
    @JoinColumn(name="creador")
    private Usuario usuario;

我正在使用Spring Data,但我有一个递归序列化问题。 当我“打印”Usuario类型的对象时,它应该序列化Usuario的所有信息。这里的要点是,Entrada(Usuario中的List)具有Usuario类型的属性。

我不想序列化Entrada的Usuario。我只是想序列化Usuario列表中的每一项。

我试图在Entrada的Usuario类型的属性中添加@JsonIgnore,但我仍然遇到同样的问题。

日志显示:

2017-12-04 18:57:46.980[0;39m [31mERROR[0;39m [35m3389[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.a.c.c.C.[.[.[/].[dispatcherServlet]   [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError) (through reference chain: es.softcorp.domains.Entrada["eventoBean"]->es.softcorp.domains.Evento["entradas"]->org.hibernate.collection.internal.PersistentBag[0]->es.softcorp.domains.Entrada["eventoBean"]->es.softcorp.domains.Evento["entradas"]->org.hibernate.collection.internal.PersistentBag[0]->es.softcorp.domains.Entrada["eventoBean"]->es.softcorp.domains.Evento["entradas"]-

等等

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题,从我记得的@JsonIgnore开始工作,你必须禁用该类的自动字段检测,或者如果你想要所有的类。

有两种方法可以解决递归问题。

  1. Json注释告诉ObjectMapper关于双向关系。
  2. @JsonIgnore@JsonView导致递归/循环的字段。
  3. 对于示例项目,我确实禁用了所有类的自动字段检测,并在我想序列化的类中使用了@JsonProperty@JsonIgnore。如果禁用自动检测,则必须手动标记字段。

    /**
     * Disable auto field detection for JSON message converter.
     * @return
     */
    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
    
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    
        objectMapper.setVisibility(
                objectMapper
                .getVisibilityChecker()
                .with(Visibility.NONE)
        );
    
        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }
    

    还有@JsonView注释与@JsonIgnore类似,可用于选择要序列化的字段,以下是进一步阅读的链接https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring

    解决问题的正确方法是告诉ObjectMapper关于递归/双向关系,如本页所述http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

    总之,您对孩子(非拥有方)使用@JsonManagedReference,并在父母(拥有关系方)上使用@JsonBackReference