首页 > 解决方案 > Spring休眠@manyToMany与@jsonView无限递归的关系

问题描述

当我尝试序列化它们时,我遇到了 spring 和 jackson 的问题我有两个类

@Entity
@Table(name = "conditions")
@Data
@JsonInclude(Include.NON_EMPTY)
@NoArgsConstructor
public class Condition implements Serializable {

    private static final long serialVersionUID = -2917417625227297889L;

    @Id
    @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
    @Column(name="nId")
    @JsonView(View.Public.class)
    private Integer id;


    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "conditions")
    @JsonView(View.Public.class)
    private Set<Rule> rules = new HashSet<Rule>(0);

    public Condition(int conditionId) {
        this.id = conditionId;
    }

}

@Entity
@Table(name = "tblComplianceRules")
@Data
@NoArgsConstructor
public class Rule implements Serializable {

    private static final long serialVersionUID = -4443637570696913241L;

    @Id
    @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
    @Column(nullable = false)
    @JsonView(View.Public.class)
    private Integer id;

    @Column
    @JsonView(View.Public.class)
    @NotEmpty
    private String name;


    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "tblComplianceRulesConditions", catalog = "swat", joinColumns = {
            @JoinColumn(name = "ruleId", nullable = false, updatable = false) }, inverseJoinColumns = {
                    @JoinColumn(name = "conditionId", nullable = false, updatable = false) })
    @JsonView(View.Public.class)
    private Set<Condition> conditions = new HashSet<Condition>();

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "rules")
    @JsonBackReference
    @JsonView(View.Internal.class)
    private Set<CompliancePolicy> policies = new HashSet<CompliancePolicy>(0);

    public Rule(Integer id) {
        this.id = id;
    }


}

也生病发布 View 类

public class View {

    public static class Public {
    }

    public static class Internal extends Public {
    }

}

我试图实现的是,我发送给客户端的响应不会发送不必要的数据,但我的标题中不断出现错误......

我能够通过不使用@JsonView 并使用@JsonBackReference 来解决这个问题,但这不是我想要的......

根据要求,我附上了回复

@GetMapping(value = "/api/condition")
    @JsonView(View.Internal.class)
    public ResponseEntity<?> getConditions(Principal user) {
        if (user != null) {
            log.debug("Getting conditions for user: " + user.getName());
        } else {
            log.error("Warning user is not defined");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        try {
            List<Condition> conditions = conditionService.getConditionsByOrderById();
            return ResponseEntity.ok().body(conditions);
        } catch (Exception ex) {
            log.error("Could not fetch condition from tblComplianceConditions: " + ExceptionUtils.getRootCauseMessage(ex), ex);
            return ResponseEntity.badRequest().body(ExceptionUtils.getRootCauseMessage(ex));
        }

    }

标签: hibernatespring-bootjackson

解决方案


我发现使用@JsonIgnore 来获取我需要的属性更容易我添加了一个@transient 字段并将其添加到那里。


推荐阅读