Ado, directly on the code, source code is a network between Mu class teacher shadow wind months to write, I used the direct use.
Package com.yb.entity; Import java.util.List; Import com.fasterxml.jackson.databind.JsonNode; Import com.fasterxml.jackson.databind.ObjectMapper; / ** * * @Title: LeeJSONResult.java * @package com.lee.utils custom response data structure: @Description * * this class is provided to the portal, iOS, Andrews, micro-channel with the store requires a method of this class type into a data format for the gateway after receiving such data * (class, or List) * other discretion * 200: indicates success * 500: an error, an error message msg field * 501: bean validation errors, no matter how many errors are returned in map form * 502: interceptor intercepts user token error * 555: Exception thrown information Copyright *: Copyright (c) 2016 Company *: Nathan.Lee.Salvatore * * @author leechenxiang * @date 2016 Nian 4 22 afternoon 8:33:36 * @version V1.0 * / public class JsonResult { // define objects jackson Private static Final ObjectMapper MAPPER = new new ObjectMapper (); // response to a traffic state Private Integer status; // response message Private String MSG; // data response Private Object data; Private String OK; // do not use public static JsonResult build(Integer status, String msg, Object data) { return new JsonResult(status, msg, data); } public static JsonResult ok(Object data) { return new JsonResult(data); } public static JsonResult ok() { return new JsonResult(null); } public static JsonResult errorMsg(String msg) { return new JsonResult(500, msg, null); } public static JsonResult errorMap(Object data) { return new JsonResult(501, "error", data); } public static JsonResult errorTokenMsg(String msg) { return new JsonResult(502, msg, null); } public static JsonResult errorException(String msg) { return new JsonResult(555, msg, null); } public JsonResult() { } // public static LeeJSONResult build(Integer status, String msg) { // return new LeeJSONResult(status, msg, null); // } public JsonResult(Integer status, String msg, Object data) { this.status = status; this.msg = msg; this.data = data; } public JsonResult(Object data) { this.status = 200; this.msg = "OK"; this.data = data; } public Boolean isOK() { return this.status == 200; } public Integer getStatus() { return status; } public void setStatus (Status Integer) { the this .status = Status; } public String a getMsg () { return MSG; } public void setMsg (String MSG) { the this a .msg = MSG; } public Object the getData () { return Data; } public void the setData (Object Data) { the this .data = Data; } / ** * * @Description: 将json结果集转化为LeeJSONResult对象 * is a need to convert the object class * @param jsonData * @param clazz * @return * * @author leechenxiang * @date 2016 Nian 4 22 afternoon 8:34:58 * / public static JsonResult formatToPojo (String jsonData, Class <?> clazz) { the try { IF (clazz == null ) { return MAPPER.readValue (jsonData, a JsonResult. class ); } jsonNode jsonNode = MAPPER.readTree (jsonData); JsonNode data = jsonNode.get("data"); Object obj = null; if (clazz != null) { if (data.isObject()) { obj = MAPPER.readValue(data.traverse(), clazz); } else if (data.isTextual()) { obj = MAPPER.readValue(data.asText(), clazz); } } return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); } catch(Exception E) { return null ; } } / ** * * @Description: no object conversion objects * @param json * @return * * @author leechenxiang * @date 2016 Nian 4 22 afternoon 8:35:21 * / public static JsonResult format (String json) { the try { return MAPPER.readValue (JSON, a JsonResult. class ); } the catch (Exception E) { e.printStackTrace (); } return null ; } / ** * * @Description: Object is set conversion * Object is a need to convert List * @param jsonData * @param clazz * @return * * @author leechenxiang * @date 2016 Nian 4 22 afternoon 8:35:31 * / public static JsonResult formatToList (String jsonData, Class <?> clazz) { the try { JsonNode jsonNode = MAPPER.readTree (jsonData); JsonNode Data = jsonNode.get ( "Data" ); Object obj = null ; IF (data.isArray() && data.size() > 0) { obj = MAPPER.readValue(data.traverse(), MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); } return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); } catch (Exception e) { return null; } } public String getOk() { return ok; } public void setOk(String ok) { this.ok = ok; } }
When using the direct method calls to JsonResult OK, as shown:
The result is the following figure: password is not displayed because I added @JsonIgnore annotations in the entity classes password
If the package useless JsonResult class implementation, then streaking similar loading, as shown below: