本文章所用技术为SpringMVC+JSON
利用工具类轻松将json返回到页面中,而不用手动一个一个去映射
首先呢我们需要定义一个返回json的工具类Resp<T>,这个类呢,我们通常可以自由化配置,这里只举一个范例
import java.io.Serializable;import java.util.Map; /** * ?A return Util to pages * @param <T> return data type */public class Resp<T> implements Serializable { ????private static final long serialVersionUID = 1L; ????private final static String SUCCESS_CODE = "200"; ????/** ????* The status returned to the page ????*/ ???private String status; ????/** ????* Message returned to the page ????*/ ???private String message; ????/** ????* Return data to the page ????*/ ???private T data; ????/** ????* ?Other data that needs to be returned to the page ????*/ ???private Map<String,Object> rtnMap; ????public Resp() { ???????this.status = SUCCESS_CODE; ???????this.message = "SUCCESS"; ???} ????public Resp(String status, String message, T data) { ???????this.status = status; ???????this.message = message; ???????this.data = data; ???} ????public Resp(String status, String message, T data, Map<String, Object> rtnMap) { ???????this.status = status; ???????this.message = message; ???????this.data = data; ???????this.rtnMap = rtnMap; ???} ????public String getStatus() { ???????return status; ???} ????public void setStatus(String status) { ???????this.status = status; ???} ????public String getMessage() { ???????return message; ???} ????public void setMessage(String message) { ???????this.message = message; ???} ????public T getData() { ???????return data; ???} ????public void setData(T data) { ???????this.data = data; ???} ????public Map<String, Object> getRtnMap() { ???????return rtnMap; ???} ????public void setRtnMap(Map<String, Object> rtnMap) { ???????this.rtnMap = rtnMap; ???}}
在工具类创建好之后呢,我们就可以去action中测试啦,
在测试之前呢,先要准备好我们需要返回的数据,数据类型可以是各种实体类,Map对象啊等等.....
这里呢我就用我自己写好的一个bean类进行测试
@RequestMapping(value = "/jsonTest")public Resp RtnTest(){ ???// 使用Resp工具类,并设置返回数据类型 ???Resp<Object> resp = new Resp<>(); ???// 设置返回数据 ???UserInfo userInfo = new UserInfo("Liang","21","male","10086"); ???// 使用实体类换换工具转换为Map ???Map<String,Object> map = EntityToMap.ConvertToMap(userInfo); ???// 两种方式向页面返回数据 ???resp.setData(userInfo); ???resp.setRtnMap(map); ????return resp;}
到这里可能就有疑惑了 EntityToMap.ConvertToMap(userInfo);这个是用来干嘛的呢?
EntityToMap也是一个很实用的工具类,可以将实体类对象转换为map,用来对数据进行再次加工
import java.lang.reflect.Field;import java.util.HashMap;import java.util.Map; public class EntityToMap { ???/** ????* @param obj Entity class that needs to be converted ????* @return ConvertedMapObject ????*/ ???public static Map<String, Object> ConvertToMap(Object obj) { ???????Map<String, Object> rtnMap = new HashMap<>(); ???????if (obj == null) return null; ???????Field[] fields = obj.getClass().getDeclaredFields(); ???????try { ???????????for (Field field : fields) { ???????????????try { ???????????????????Field f = obj.getClass().getDeclaredField(field.getName()); ???????????????????f.setAccessible(true); ???????????????????Object o = f.get(obj); ???????????????????rtnMap.put(field.getName(), o); ???????????????} catch (Exception e) { ???????????????????e.printStackTrace(); ???????????????} ???????????} ???????} catch (SecurityException e) { ???????????e.printStackTrace(); ???????} ???????return rtnMap; ???}}
通过这两个工具类呢,是不是很方便的可以将我们的数据返回到页面中去了呢?
我们一起来看一下页面中显示的结果吧:
是不是很方便呢?
使用工具类,让Json返回更得体,更好看
原文地址:https://www.cnblogs.com/cnliang/p/9656765.html