首先:
部门与组织是manytoone和onetomany的关系,并且两张表都有自关联,查询部门信息时会级联查询上级部门以及他的组织结构,然后返回json数据时,会出现$ref
即:
为了攻破它,在两张表的onetomany那方,也就是set的那方,加一个fastjson的注解@JSONField(serialize = false)
即:
由于我组织结构那里有多个onetomany,所以我都加上了:
然后在controller里面有两种写法:
一:在方法前面加 @ResponseBody,再直接返回一个对象
/**
* 部门信息列表展示
* @param response
*/
@ResponseBody
@RequestMapping(value = "/list", method = RequestMethod.GET)
public PageView list(HttpServletResponse response, Integer pageSize, Integer page) {
PageView departmentPageView = departmentService.queryBySerach(null, page, pageSize);
//ajaxJson(JSON.toJSONString(departmentPageView, SerializerFeature.DisableCircularReferenceDetect), response);
return departmentPageView;
}
然后在配置文件里配置:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8value>
list>
property>
<property name="features">
<array>
<value>DisableCircularReferenceDetectvalue>
array>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
<bean id="DisableCircularReferenceDetect" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="com.alibaba.fastjson.serializer.SerializerFeature.DisableCircularReferenceDetect">property>
bean>
在使用Spring注入时,我们将类信息注入到com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter
这个类中。
在源码中可以看到:
@Override
protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {
OutputStream out = outputMessage.getBody();
String text = JSON.toJSONString(obj, features);
byte[] bytes = text.getBytes(charset);
out.write(bytes);
}
我们使用@ResponseBody这个注解时,会自动调用JSON.toJSONString(obj,features)这个方法,里面会带上我们在Spring里面配置的属性。
附一种不好的编程习惯:
在加了@ResponseBody注解的Controller中使用
String result = JSON.toJSONString(obj);
return result;
JSON.toJSONString() 这句话执行了两次。
方案二:不用配置文件,直接转SerializerFeature.DisableCircularReferenceDetect
即:
/**
* 部门信息列表展示
* @param response
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public void list(HttpServletResponse response, Integer pageSize, Integer page) {
PageView departmentPageView = departmentService.queryBySerach(null, page, pageSize);
ajaxJson(JSON.toJSONString(departmentPageView, SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.WriteMapNullValue), response);
}
然后ajaxJson方法是:
@Controller
public class BaseController
{
protected static final Log log = LogFactory.getLog(BaseController.class);
/** ajax输出,返回null ***/
public String ajax(String content, String type, HttpServletResponse response)
{
try
{
response.setContentType(type + ";charset=UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.getWriter().write(content);
response.getWriter().flush();
}
catch (IOException e)
{
log.error("ajax", e);
}
return null;
}