There is a cycle in the hierarchy解决

前言:

  在一次项目中,分页查询公告列表信息后,在遍历查询到的公告列表时出现了死循环“There is a cycle in the hierarchy”错误,分析原因是因为在公告实体类中包含了商铺对象导致,所以在遍历的时候需要过滤掉商铺这个字段。

代码示例:

公告实体类

/**
 * 
 * 电商-公告
 * EshopNotice entity.*/
@Entity
@Table(name = "eshop_notice")
@JsonIgnoreProperties(value={"shop"})
public class EshopNotice implements java.io.Serializable {

    // Fields

    // 系统ID     
    private String sysid;
    
    //时间戳记
    private String tstamp;
    
    // 操作日期
    private String operationDateTime;
    
    // 操作员
    private String operator;
    
    /**
     * 商铺
     */
    private CoreCompany shop;
    
    /**
     * 标题
     */
    private String title;
    
    /**
     * 内容
     */
    private String content;
    
    /**
     * 发布日期
     */
    private String publishDatetime;
    
    /**
     * 状态
     */
    private String status;

    // Constructors
    /** default constructor */
    public EshopNotice() {
    }
    /** minimal constructor */
    public EshopNotice(String tstamp, String operationDateTime, String operator, String title, String content,
            String publishDatetime, String status) {
        this.tstamp = tstamp;
        this.operationDateTime = operationDateTime;
        this.operator = operator;
        this.title = title;
        this.content = content;
        this.publishDatetime = publishDatetime;
        this.status = status;
    }

    /** full constructor */
    public EshopNotice(String tstamp, String operationDateTime, String operator, CoreCompany shop, String title,
            String content, String publishDatetime, String status) {
        this.tstamp = tstamp;
        this.operationDateTime = operationDateTime;
        this.operator = operator;
        this.shop = shop;
        this.title = title;
        this.content = content;
        this.publishDatetime = publishDatetime;
        this.status = status;
    }

    // Property accessors
    @GenericGenerator(name = "generator", strategy = "uuid.hex")
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "sysid", unique = true, nullable = false, length = 32)
    public String getSysid() {
        return sysid;
    }
    public void setSysid(String sysid) {
        this.sysid = sysid;
    }    
    @Column(name = "tstamp", nullable = false, length = 20)
    public String getTstamp() {
        return tstamp;
    }
    public void setTstamp(String tstamp) {
        this.tstamp = tstamp;
    }
    @Column(name = "operationdatetime", nullable = false, length = 20)
    public String getOperationDateTime() {
        return operationDateTime;
    }
    public void setOperationDateTime(String operationDateTime) {
        this.operationDateTime = operationDateTime;
    }
    @Column(name = "operator", nullable = false, length = 32)
    public String getOperator() {
        return this.operator;
    }
    public void setOperator(String operator) {
        this.operator = operator;
    }  
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "shop", nullable = false)
    public CoreCompany getShop() {
        return this.shop;
    }
    public void setShop(CoreCompany shop) {
        this.shop = shop;
    }
    @Column(name = "title", nullable = false, length = 128)
    public String getTitle() {
        return this.title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    @Column(name = "content", nullable = false, length = 2000)
    public String getContent() {
        return this.content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    @Column(name = "publishdatetime", nullable = false, length = 20)
    public String getPublishDatetime() {
        return publishDatetime;
    }
    public void setPublishDatetime(String publishDatetime) {
        this.publishDatetime = publishDatetime;
    }
    @Column(name = "status", nullable = false, length = 32)
    public String getStatus() {
        return this.status;
    }
    public void setStatus(String status) {
        this.status = status;
    }

分页查询遍历

@RequestMapping("/listPage.html")
    public JSONTableDateView noticeList(HttpServletRequest request,PageQuery pageQuery)  {    
        
        CoreMember member=(CoreMember)request.getSession().getAttribute("member");
        CoreCompany company=coreCompanyService.getByMemberId(member.getSysid());
                
      //分页查询
      PageResults<EshopNotice> pageResults = noticeService.noticeList(pageQuery,company.getSysid());
      //设置页面参数
      JSONArray data = new JSONArray();
      for(EshopNotice eshopNotice : pageResults.getResults()){
        JsonConfig jsonConfig=new JsonConfig();
        jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
                @Override
                public boolean apply(Object arg0, String arg1, Object arg2) {
                  //过滤段公告中的shop字段,否则会无限死循环
                    if (arg1.equals("shop") ) {
                        return true;
                    } else {
                        return false;
                    }
                }
            });
        JSONObject dataTemp =JSONObject.fromObject(eshopNotice,jsonConfig);
        dataTemp.put("title", eshopNotice.getTitle());
        dataTemp.put("content", eshopNotice.getContent());
        if(eshopNotice.getStatus().equals("00")){
             dataTemp.put("status","申请");
        }else{
            dataTemp.put("status","审核通过");
        }       
        dataTemp.put("publishDatetime",eshopNotice.getPublishDatetime());
        dataTemp.put("sysid", eshopNotice.getSysid());
        data.add(dataTemp);
      }
      JSONTableDateView jSONTableDateView  = new JSONTableDateView(pageQuery, pageResults, data);     
      return jSONTableDateView;
    }        
    

 

转载于:https://www.cnblogs.com/guzhou-ing/p/7201612.html

### JSONException 的原因及解决方案 #### 错误原因分析 `JSONException` 是 JSON 解析过程中常见的异常,通常表示解析的字符串不符合标准的 JSON 格式。以下是几个常见场景及其可能的原因: 1. **循环引用问题** 当对象层次结构中存在循环依赖时,JSON 序列化工具无法正常处理这种关系,从而抛出 `net.sf.json.JSONException: There is a cycle in the hierarchy!` 异常[^1]。 2. **语法错误** 如果输入的字符串不是合法的 JSON 数据,则会触发类似于 `com.alibaba.fastjson.JSONException: syntax error, pos 1, line 1, column` 这样的异常[^2]。这可能是由于缺少括号、引号不匹配或其他格式问题引起的。 3. **未闭合字符串** 另一种情况是当 JSON 字符串中的某些部分未能正确结束(例如双引号缺失),则会出现 `com.alibaba.fastjson.JSONException: unclosed string` 提示[^3]。 #### 解决方案详解 针对上述提到的各种类型的 `JSONException`,可以采取如下措施来解决问题: 1. **对于循环引用的情况** 需要修改数据模型或者调整序列化的配置选项以打破循环链路。比如,在使用 FastJSON 时可以通过设置参数忽略特定字段或禁用循环检测功能: ```java JSONObject.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect); ``` 2. **修复非法字符引发的语法错误** 确认传入的数据确实遵循 RFC 4627 定义的标准形式;必要时候借助正则表达式清理脏数据后再传递给解析器尝试重新加载。 3. **补全遗漏的终止标记** 检查原始文本是否存在任何悬而未决的状态(如开放的大括号 `{` 或者方括号 `[` 而无对应的关闭符号)。一旦发现此类状况即刻修正相应位置的内容直至满足完整的语法规则为止。 通过以上方法能够有效应对大部分由 `JSONException` 所带来的挑战并保障程序稳定运行。 ```python import json try: data = '{"key": "value"' parsed_data = json.loads(data) # This will raise an exception due to missing closing brace. except ValueError as e: print(f"Parsing failed: {e}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值