fastjson @ResponseBody 导致实体顺序混乱问题解决

2022-07-26,,,,

实体内字段有将近几百个 使用 fastjson 或者 @ResponseBody 给前端返回json 数据发现 集合内对象的字段和自己定义的实体顺序不一致。最后使用 GSON 解决。下面是使用到的jar包。
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

	List<Test> test = testService.selectAll();
	// 让浏览器用utf8来解析返回的数据
	 response.setHeader("Content-type", "text/html;charset=UTF-8");
	// 告诉servlet用UTF-8转码,而不是用默认的ISO8859
	response.setCharacterEncoding("UTF-8");
	// 不需要返回值
	Gson gson2=new GsonBuilder().serializeNulls().create();
	String str=gson2.toJson(test);
	System.out.println("json>>>"+str);
	response.getWriter().write(str);

本文地址:https://blog.csdn.net/aiyang6666/article/details/110481681

《fastjson @ResponseBody 导致实体顺序混乱问题解决.doc》

下载本文的Word格式文档,以方便收藏与打印。