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:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> <value>application/xml;charset=UTF-8</value> </list> </property> <property name="features"> <array> <value>DisableCircularReferenceDetect</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- Solution use fastJson Appear in $ref problem(Quote) --> <!-- jQuery This front-end technology cannot resolve the reference --> <!-- Disable circular reference detection --> <bean id="DisableCircularReferenceDetect" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="staticField" value="com.alibaba.fastjson.serializer.SerializerFeature.DisableCircularReferenceDetect"> </property> </bean>
The results are as follows: