【问题标题】:Spring @JsonIgnore Serialization not working as expectedSpring @JsonIgnore 序列化未按预期工作
【发布时间】:2018-05-17 06:26:43
【问题描述】:

我有两个 Java 类:Usuario 和 Entrada。

乌苏里奥:

@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;

英特拉达:

@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 (List in Usuario) 有一个 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"]-

等等

【问题讨论】:

  • 此链接baeldung.com/… 可能会有所帮助
  • print 是什么意思?
  • 你能提供更多的异常日志和Evento类吗?
  • 错误消息说这是由于通过“entradas”变量进行的无限递归,但您只使用@JsonIgnore 注释了“compraEntradas”......
  • 由于错误,您在此处多了一个循环:es.softcorp.domains.Entrada["eventoBean"]-&gt;es.softcorp.domains.Evento["entradas"]。尝试忽略这一点。

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


【解决方案1】:

我已经解决了这个问题,从我记得@JsonIgnore 的工作中,您必须禁用该类的自动字段检测,或者如果您希望所有类都禁用。

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

  1. Json 注释告诉 ObjectMapper 双向关系。
  2. @JsonIgnore, @JsonView 导致递归/循环的字段。

对于一个示例项目,我为所有此类禁用了自动字段检测,并在我想要序列化的类中使用了@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 2018-10-05
    • 2015-04-01
    • 1970-01-01
    • 2016-01-18
    • 2016-02-27
    • 2020-01-18
    相关资源
    最近更新 更多