1、如果任务下来了,并且给定了你指定格式的JSON数据类型,那么就要想法封装成此种JSON格式的数据类型,方便其他成员进行调用,那么是如何进行封装的呢,这里简单研究一下子。

 

2、如果文档指定的封装类型是下面,这样格式的,应该如何进行封装呢?

  1. 1 {
  2. 2 "code": 0,
  3. 3 "msg": "success",
  4. 4 "data": {
  5. 5 "id": 2,
  6. 6 "account": "张三",
  7. 7 "cname": "张三",
  8. 8 "sex": "",
  9. 9 "password": "123456",
  10. 10 "identity": "415555555555555552",
  11. 11 "telephone": "15255555555",
  12. 12 "address": "河南省商丘市",
  13. 13 "birthday": "1999-06-15",
  14. 14 "identification": "1"
  15. 15 }
  16. 16 }

2.1、那么可以封装一个工具类,定义三个参数,分别是code、msg、data(这里使用的是Object类型的,你也可以设置成泛型的,看自己的喜好了)。然后创建几个调用成功,失败,或者自己构建一个方法,将参数传递进去即可。

  1. 1 package com.bie.demo.utils;
  2. 2
  3. 3 import com.fasterxml.jackson.databind.JsonNode;
  4. 4 import com.fasterxml.jackson.databind.ObjectMapper;
  5. 5
  6. 6 import java.io.Serializable;
  7. 7 import java.util.List;
  8. 8
  9. 9 /**
  10. 10 * @ProjectName:
  11. 11 * @Package:
  12. 12 * @ClassName:
  13. 13 * @Author:
  14. 14 * @Description: ${description}
  15. 15 * @Date: 2020/2/28 10:40
  16. 16 * @Version: 1.0
  17. 17 */
  18. 18 public class NationalPolicyResult implements Serializable {
  19. 19
  20. 20 /**
  21. 21 *
  22. 22 */
  23. 23 private static final long serialVersionUID = 1L;
  24. 24
  25. 25 // 定义jackson对象
  26. 26 private static final ObjectMapper MAPPER = new ObjectMapper();
  27. 27
  28. 28 // 返回标记,成功标记为0,失败为1
  29. 29 private Integer code;
  30. 30
  31. 31 // 返回消息
  32. 32 private String msg;
  33. 33
  34. 34 // 返回中的数据
  35. 35 private Object data;
  36. 36
  37. 37 /**
  38. 38 * 1、成功返回调用的方法
  39. 39 *
  40. 40 * @param data
  41. 41 * @return
  42. 42 */
  43. 43 public static NationalPolicyResult success(Object data) {
  44. 44 return new NationalPolicyResult(data);
  45. 45 }
  46. 46
  47. 47 /**
  48. 48 * 2、成功返回调用的方法,重载方法
  49. 49 *
  50. 50 * @return
  51. 51 */
  52. 52 public static NationalPolicyResult success() {
  53. 53 return new NationalPolicyResult(null);
  54. 54 }
  55. 55
  56. 56
  57. 57 /**
  58. 58 * 3、传入封装的数据,返回标记和返回信息进行默认
  59. 59 *
  60. 60 * @param data
  61. 61 */
  62. 62 public NationalPolicyResult(Object data) {
  63. 63 this.code = 0;
  64. 64 this.msg = "success";
  65. 65 this.data = data;
  66. 66 }
  67. 67
  68. 68 /**
  69. 69 * 4、无参的构造方法
  70. 70 */
  71. 71 public NationalPolicyResult() {
  72. 72
  73. 73 }
  74. 74
  75. 75 /**
  76. 76 * 5、自己构建一个方法,调用构造方法,返回自己封装的状态,返回信息,和封装的数据信息
  77. 77 *
  78. 78 * @param code
  79. 79 * @param msg
  80. 80 * @param data
  81. 81 * @return
  82. 82 */
  83. 83 public static NationalPolicyResult build(Integer code, String msg, Object data) {
  84. 84 return new NationalPolicyResult(code, msg, data);
  85. 85 }
  86. 86
  87. 87 /**
  88. 88 * 6、自己构建一个方法,重载,调用构造方法,默认封装的数据信息为null
  89. 89 *
  90. 90 * @param code
  91. 91 * @param msg
  92. 92 * @return
  93. 93 */
  94. 94 public static NationalPolicyResult build(Integer code, String msg) {
  95. 95 return new NationalPolicyResult(code, msg, null);
  96. 96 }
  97. 97
  98. 98 /**
  99. 99 * 7、可以传入封装的数据,和封装的信息,失败或者成功
  100. 100 *
  101. 101 * @param data
  102. 102 * @param msg
  103. 103 */
  104. 104 public NationalPolicyResult(Object data, String msg) {
  105. 105 this.code = 0;
  106. 106 this.msg = msg;
  107. 107 this.data = data;
  108. 108 }
  109. 109
  110. 110
  111. 111 /**
  112. 112 * 8、含参的构造方法
  113. 113 *
  114. 114 * @param code
  115. 115 * @param msg
  116. 116 * @param data
  117. 117 */
  118. 118 public NationalPolicyResult(Integer code, String msg, Object data) {
  119. 119 this.code = code;
  120. 120 this.msg = msg;
  121. 121 this.data = data;
  122. 122 }
  123. 123
  124. 124 public Integer getCode() {
  125. 125 return code;
  126. 126 }
  127. 127
  128. 128 public void setCode(Integer code) {
  129. 129 this.code = code;
  130. 130 }
  131. 131
  132. 132 public String getMsg() {
  133. 133 return msg;
  134. 134 }
  135. 135
  136. 136 public void setMsg(String msg) {
  137. 137 this.msg = msg;
  138. 138 }
  139. 139
  140. 140 public Object getData() {
  141. 141 return data;
  142. 142 }
  143. 143
  144. 144 public void setData(Object data) {
  145. 145 this.data = data;
  146. 146 }
  147. 147
  148. 148
  149. 149 /**
  150. 150 * 将json结果集转化为NationalPolicyResult对象
  151. 151 *
  152. 152 * @param jsonData json数据
  153. 153 * @param clazz NationalPolicyResult中的object类型
  154. 154 * @return
  155. 155 */
  156. 156 public static NationalPolicyResult formatToPojo(String jsonData, Class<?> clazz) {
  157. 157 try {
  158. 158 if (clazz == null) {
  159. 159 return MAPPER.readValue(jsonData, NationalPolicyResult.class);
  160. 160 }
  161. 161 JsonNode jsonNode = MAPPER.readTree(jsonData);
  162. 162 JsonNode data = jsonNode.get("data");
  163. 163 Object obj = null;
  164. 164 if (clazz != null) {
  165. 165 if (data.isObject()) {
  166. 166 obj = MAPPER.readValue(data.traverse(), clazz);
  167. 167 } else if (data.isTextual()) {
  168. 168 obj = MAPPER.readValue(data.asText(), clazz);
  169. 169 }
  170. 170 }
  171. 171 return build(jsonNode.get("code").intValue(), jsonNode.get("msg").asText(), obj);
  172. 172 } catch (Exception e) {
  173. 173 return null;
  174. 174 }
  175. 175 }
  176. 176
  177. 177 /**
  178. 178 * 没有object对象的转化
  179. 179 *
  180. 180 * @param json
  181. 181 * @return
  182. 182 */
  183. 183 public static NationalPolicyResult format(String json) {
  184. 184 try {
  185. 185 return MAPPER.readValue(json, NationalPolicyResult.class);
  186. 186 } catch (Exception e) {
  187. 187 e.printStackTrace();
  188. 188 }
  189. 189 return null;
  190. 190 }
  191. 191
  192. 192 /**
  193. 193 * Object是集合转化
  194. 194 *
  195. 195 * @param jsonData json数据
  196. 196 * @param clazz 集合中的类型
  197. 197 * @return
  198. 198 */
  199. 199 public static NationalPolicyResult formatToList(String jsonData, Class<?> clazz) {
  200. 200 try {
  201. 201 JsonNode jsonNode = MAPPER.readTree(jsonData);
  202. 202 JsonNode data = jsonNode.get("data");
  203. 203 Object obj = null;
  204. 204 if (data.isArray() && data.size() > 0) {
  205. 205 obj = MAPPER.readValue(data.traverse(),
  206. 206 MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
  207. 207 }
  208. 208 return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
  209. 209 } catch (Exception e) {
  210. 210 return null;
  211. 211 }
  212. 212 }
  213. 213
  214. 214 }

2.2、将查询返回的结果进行封装返回,如果失败了,或者成功了,如何进行调用。如下所示:

  1. 1 package com.bie.demo.controller;
  2. 2
  3. 3 import com.bie.demo.po.CustomerInfo;
  4. 4 import com.bie.demo.service.CustomerInfoService;
  5. 5 import com.bie.demo.utils.NationalPolicyResult;
  6. 6 import org.springframework.beans.factory.annotation.Autowired;
  7. 7 import org.springframework.stereotype.Controller;
  8. 8 import org.springframework.web.bind.annotation.RequestMapping;
  9. 9 import org.springframework.web.bind.annotation.RequestParam;
  10. 10 import org.springframework.web.bind.annotation.ResponseBody;
  11. 11
  12. 12 /**
  13. 13 *
  14. 14 */
  15. 15 @Controller
  16. 16 @RequestMapping(value = "/customerInfo")
  17. 17 public class CustomerInfoController {
  18. 18
  19. 19 @Autowired
  20. 20 private CustomerInfoService customerInfoService;
  21. 21
  22. 22 @RequestMapping(value = "/selectCustomerInfoById")
  23. 23 @ResponseBody
  24. 24 public NationalPolicyResult selectCustomerInfoById(@RequestParam(value = "id") int id) {
  25. 25 CustomerInfo customerInfo = customerInfoService.selectCustomerInfoById(id);
  26. 26 NationalPolicyResult nationalPolicyResult = new NationalPolicyResult();
  27. 27 NationalPolicyResult result = null;
  28. 28 if (customerInfo != null) {
  29. 29 result = nationalPolicyResult.success(customerInfo);
  30. 30 } else {
  31. 31 result = nationalPolicyResult.build(1, "失败了.......");
  32. 32 }
  33. 33 return result;
  34. 34 }
  35. 35
  36. 36
  37. 37 }

2.3、页面调用一下,看看是否正确的返回结果。

使用json在线解析,查看是否是正确的json格式。

 

3、如果文档指定的封装类型是下面,这样格式的,应该如何进行封装呢?

  1. 1 {
  2. 2 "code": 0,
  3. 3 "msg": "success",
  4. 4 "data": {
  5. 5 "records": [{
  6. 6 "id": 1,
  7. 7 "account": "admin",
  8. 8 "cname": "admin",
  9. 9 "sex": "",
  10. 10 "password": "123456",
  11. 11 "identity": "415555555555555551",
  12. 12 "telephone": "15255555555",
  13. 13 "address": "河南省新乡市",
  14. 14 "birthday": "1999-06-15",
  15. 15 "identification": "1"
  16. 16 }, {
  17. 17 "id": 2,
  18. 18 "account": "张三",
  19. 19 "cname": "张三",
  20. 20 "sex": "",
  21. 21 "password": "123456",
  22. 22 "identity": "415555555555555552",
  23. 23 "telephone": "15255555555",
  24. 24 "address": "河南省商丘市",
  25. 25 "birthday": "1999-06-15",
  26. 26 "identification": "1"
  27. 27 }],
  28. 28 "total": 100,
  29. 29 "size": 20,
  30. 30 "current": 1,
  31. 31 "orders": [],
  32. 32 "searchCount": true,
  33. 33 "pages": 23
  34. 34 }
  35. 35 }

3.1、当然了,上面那个封装的也要接着使用,还需要再封装一个。那么可以再封装一个工具类,定义七个参数,分别是records、total、size、current、orders、searchCount、pages。

  1. 1 package com.bie.demo.utils;
  2. 2
  3. 3
  4. 4 import com.bie.demo.po.CustomerInfo;
  5. 5
  6. 6 import java.util.Arrays;
  7. 7 import java.util.List;
  8. 8
  9. 9 /**
  10. 10 *
  11. 11 */
  12. 12 public class CustomerInfoResult {
  13. 13
  14. 14 private List<CustomerInfo> records;
  15. 15 private long total;
  16. 16 private int size;
  17. 17 private int current;
  18. 18 private int[] orders;
  19. 19 private boolean searchCount;
  20. 20 private long pages;
  21. 21
  22. 22 public List<CustomerInfo> getRecords() {
  23. 23 return records;
  24. 24 }
  25. 25
  26. 26 public void setRecords(List<CustomerInfo> records) {
  27. 27 this.records = records;
  28. 28 }
  29. 29
  30. 30 public long getTotal() {
  31. 31 return total;
  32. 32 }
  33. 33
  34. 34 public void setTotal(long total) {
  35. 35 this.total = total;
  36. 36 }
  37. 37
  38. 38 public int getSize() {
  39. 39 return size;
  40. 40 }
  41. 41
  42. 42 public void setSize(int size) {
  43. 43 this.size = size;
  44. 44 }
  45. 45
  46. 46 public int getCurrent() {
  47. 47 return current;
  48. 48 }
  49. 49
  50. 50 public void setCurrent(int current) {
  51. 51 this.current = current;
  52. 52 }
  53. 53
  54. 54 public int[] getOrders() {
  55. 55 return orders;
  56. 56 }
  57. 57
  58. 58 public void setOrders(int[] orders) {
  59. 59 this.orders = orders;
  60. 60 }
  61. 61
  62. 62 public boolean isSearchCount() {
  63. 63 return searchCount;
  64. 64 }
  65. 65
  66. 66 public void setSearchCount(boolean searchCount) {
  67. 67 this.searchCount = searchCount;
  68. 68 }
  69. 69
  70. 70 public long getPages() {
  71. 71 return pages;
  72. 72 }
  73. 73
  74. 74 public void setPages(long pages) {
  75. 75 this.pages = pages;
  76. 76 }
  77. 77
  78. 78 @Override
  79. 79 public String toString() {
  80. 80 return "CustomerInfoResult{" +
  81. 81 "records=" + records +
  82. 82 ", total=" + total +
  83. 83 ", size=" + size +
  84. 84 ", current=" + current +
  85. 85 ", orders=" + Arrays.toString(orders) +
  86. 86 ", searchCount=" + searchCount +
  87. 87 ", pages=" + pages +
  88. 88 \'}\';
  89. 89 }
  90. 90 }

3.2、将查询返回的结果进行封装返回,最后再次进行封装,得到你想要的格式即可,如果失败了,或者成功了,如何进行调用。如下所示:

主要根据自己想要的格式进行封装哈。

  1. 1 package com.bie.demo.controller;
  2. 2
  3. 3 import com.bie.demo.po.CustomerInfo;
  4. 4 import com.bie.demo.service.CustomerInfoService;
  5. 5 import com.bie.demo.utils.CustomerInfoResult;
  6. 6 import com.bie.demo.utils.NationalPolicyResult;
  7. 7 import org.springframework.beans.factory.annotation.Autowired;
  8. 8 import org.springframework.stereotype.Controller;
  9. 9 import org.springframework.web.bind.annotation.RequestMapping;
  10. 10 import org.springframework.web.bind.annotation.RequestParam;
  11. 11 import org.springframework.web.bind.annotation.ResponseBody;
  12. 12
  13. 13 import java.util.List;
  14. 14
  15. 15 /**
  16. 16 *
  17. 17 */
  18. 18 @Controller
  19. 19 @RequestMapping(value = "/customerInfo")
  20. 20 public class CustomerInfoController {
  21. 21
  22. 22 @Autowired
  23. 23 private CustomerInfoService customerInfoService;
  24. 24
  25. 25
  26. 26 @RequestMapping(value = "/selectCustomerInfoAll")
  27. 27 @ResponseBody
  28. 28 public NationalPolicyResult selectCustomerInfoAll() {
  29. 29 // 查询返回所有的数据
  30. 30 List<CustomerInfo> customerInfos = customerInfoService.selectCustomerInfoAll();
  31. 31 // 封装指定的json格式数据
  32. 32 CustomerInfoResult customerInfoResult = new CustomerInfoResult();
  33. 33 customerInfoResult.setRecords(customerInfos);
  34. 34 customerInfoResult.setTotal(100);
  35. 35 customerInfoResult.setSize(20);
  36. 36 customerInfoResult.setCurrent(1);
  37. 37 customerInfoResult.setOrders(new int[0]);
  38. 38 customerInfoResult.setSearchCount(true);
  39. 39 customerInfoResult.setPages(23);
  40. 40
  41. 41 // 再次封装指定的json格式数据
  42. 42 NationalPolicyResult nationalPolicyResult = new NationalPolicyResult();
  43. 43 NationalPolicyResult result = null;
  44. 44 if (customerInfos != null && customerInfos.size() > 0 && !customerInfos.isEmpty()) {
  45. 45 result = nationalPolicyResult.success(customerInfoResult);
  46. 46 } else {
  47. 47 result = nationalPolicyResult.build(1, "失败了.......");
  48. 48 }
  49. 49 return result;
  50. 50 }
  51. 51 }

3.3、页面调用一下,看看是否正确的返回结果。

使用json在线解析,查看是否是正确的json格式。 

那么快根据你的文档需求进行JSON封装吧。

 

作者:别先生

博客园:https://www.cnblogs.com/biehongli/

如果您想及时得到个人撰写文章以及著作的消息推送,可以扫描上方二维码,关注个人公众号哦。

 

版权声明:本文为biehongli原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/biehongli/p/12386082.html