$ref: problem in converting object to json data in springmvc

Keywords: Java Spring JSON xml

Today, when writing code, some data was found to be scrambled during request fetching, as shown in the figure

Entity class A

public class BaseItems{

    private Integer id;
    //Other attributes omitted
    private Set<BasePolicy> policys;

Entity class B

public class BasePolicy implements java.io.Serializable {

    private Long id;
    private String grade;  //File level
    private String title;//Title
    private String fileno; //Symbol
    private String dispatchDep; //Dispatch unit
    //Other attributes omitted

The relationship of a b is one to many. When querying a data, the data of associated b will be queried together

Reason for the problem: there is a circular reference problem when serializing the list data json. In short, circular reference is caused by the mutual reference of multiple elements / attributes in the collection / object.

Solution: add the following code to the spring configuration file

    <mvc:message-converters register-defaults="true">
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            <property name="supportedMediaTypes">
            <property name="features">
<!-- Solution use fastJson Appear in $ref problem(Quote)  -->
<!-- jQuery This front-end technology cannot resolve the reference -->
<!-- Disable circular reference detection -->
<bean id="DisableCircularReferenceDetect"
    <property name="staticField"

The results are as follows:

Posted by 9three on Wed, 06 Nov 2019 14:03:02 -0800