RestApiIOUtils.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.primeton.dgs.web.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.google.common.net.HttpHeaders;
  6. import com.google.common.net.MediaType;
  7. import org.apache.commons.io.IOUtils;
  8. import org.apache.commons.lang.StringUtils;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13. import java.io.StringReader;
  14. import java.io.Writer;
  15. import java.lang.reflect.Array;
  16. import java.util.Collection;
  17. import java.util.List;
  18. /**
  19. *
  20. * Rest 服务转换. Add:METQI-45,Http RestApi 输入输出工具类
  21. *
  22. * <pre>
  23. *
  24. * Created by zhaopx.
  25. * User: zhaopx
  26. * Date: 2019/9/5
  27. * Time: 09:21
  28. *
  29. * </pre>
  30. *
  31. * @author zhaopx
  32. */
  33. public class RestApiIOUtils {
  34. /**
  35. * 把 Java Bean 转为 JSON 字符串
  36. * @param bean
  37. * @return
  38. */
  39. public static String convertString(Object bean) {
  40. if(bean instanceof JSON) {
  41. // 如果是 JSON 对象
  42. return ((JSON)bean).toJSONString();
  43. }
  44. if(bean instanceof Array || bean instanceof Collection) {
  45. // 集合类型、数组类型返回 array 数组
  46. return JSONArray.toJSONString(bean);
  47. }
  48. return JSONObject.toJSONString(bean);
  49. }
  50. /**
  51. * 把 Java Bean 转为 JSON 字符串
  52. * @param bean
  53. * @return
  54. */
  55. public static <T> void output(T bean, OutputStream out) throws IOException {
  56. IOUtils.copy(new StringReader(convertString(bean)), out);
  57. }
  58. /**
  59. * 把 Java Bean 转为 JSON 字符串
  60. * @param bean
  61. * @return
  62. */
  63. public static void output(Object bean, Writer out) throws IOException {
  64. IOUtils.copy(new StringReader(convertString(bean)), out);
  65. }
  66. /**
  67. * 把 Java Bean 转为 JSON 字符串,并从 HttpServletResponse 输出
  68. * @param bean
  69. * @return
  70. */
  71. public static void output(Object bean,
  72. HttpServletResponse response) {
  73. output(bean, response, "UTF-8");
  74. }
  75. /**
  76. * 把 Java Bean 转为 JSON 字符串,并从 HttpServletResponse 输出
  77. * @param bean
  78. * @return
  79. */
  80. public static void output(Object bean,
  81. HttpServletResponse response,
  82. String charset) {
  83. response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString());
  84. response.setCharacterEncoding(charset);
  85. try {
  86. IOUtils.copy(new StringReader(convertString(bean)), response.getWriter());
  87. } catch (IOException e) {
  88. throw new IllegalStateException(e);
  89. }
  90. }
  91. /**
  92. * 把 Rest API 中提交的 JSON 表单数据,转为 clazz 指定的 Bean 类型的 LIST 集合
  93. * @param request HttpRestAPI
  94. * * @param clazz JSON 能转换的 Class
  95. * @return
  96. */
  97. public static <T> List<T> beanArray(HttpServletRequest request,
  98. Class<T> clazz) {
  99. String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
  100. try {
  101. String json = StringUtils.trim(IOUtils.toString(request.getReader()));
  102. // Rest API 接口
  103. if(json.startsWith("[") && json.endsWith("]")) {
  104. return JSONObject.parseArray(json, clazz);
  105. }
  106. throw new IllegalArgumentException("错误的数组消息实体。" + json);
  107. } catch (IOException e) {
  108. throw new IllegalStateException(e);
  109. }
  110. }
  111. /**
  112. * 把 Rest API 中提交的 JSON 表单数据,转为 clazz 指定的 Bean 类型
  113. * @param request HttpRestAPI
  114. * @param clazz JSON 能转换的 Class
  115. * @return
  116. */
  117. public static <T> T beanObject(HttpServletRequest request,
  118. Class<T> clazz) {
  119. String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
  120. try {
  121. String json = StringUtils.trim(IOUtils.toString(request.getReader()));
  122. // Rest API 接口
  123. if(json.startsWith("{") && json.endsWith("}")) {
  124. return JSONObject.parseObject(json, clazz);
  125. }
  126. throw new IllegalArgumentException("错误的消息对象实体。" + json);
  127. } catch (IOException e) {
  128. throw new IllegalStateException(e);
  129. }
  130. }
  131. /**
  132. * 把 Rest API 中提交的 JSON 表单数据,转为 JSON 类型
  133. * @param request HttpRestAPI
  134. * @return
  135. */
  136. public static JSON jsonObject(HttpServletRequest request) {
  137. String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
  138. try {
  139. String json = StringUtils.trim(IOUtils.toString(request.getReader()));
  140. // Rest API 接口
  141. if(json.startsWith("{") && json.endsWith("}")) {
  142. return JSONObject.parseObject(json);
  143. } else if(json.startsWith("[") && json.endsWith("]")) {
  144. return JSON.parseArray(json);
  145. }
  146. throw new IllegalArgumentException("错误的消息对象实体。" + json);
  147. } catch (IOException e) {
  148. throw new IllegalStateException(e);
  149. }
  150. }
  151. }