123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package com.primeton.dgs.web.utils;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.google.common.net.HttpHeaders;
- import com.google.common.net.MediaType;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.lang.StringUtils;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.StringReader;
- import java.io.Writer;
- import java.lang.reflect.Array;
- import java.util.Collection;
- import java.util.List;
- /**
- *
- * Rest 服务转换. Add:METQI-45,Http RestApi 输入输出工具类
- *
- * <pre>
- *
- * Created by zhaopx.
- * User: zhaopx
- * Date: 2019/9/5
- * Time: 09:21
- *
- * </pre>
- *
- * @author zhaopx
- */
- public class RestApiIOUtils {
- /**
- * 把 Java Bean 转为 JSON 字符串
- * @param bean
- * @return
- */
- public static String convertString(Object bean) {
- if(bean instanceof JSON) {
- // 如果是 JSON 对象
- return ((JSON)bean).toJSONString();
- }
- if(bean instanceof Array || bean instanceof Collection) {
- // 集合类型、数组类型返回 array 数组
- return JSONArray.toJSONString(bean);
- }
- return JSONObject.toJSONString(bean);
- }
- /**
- * 把 Java Bean 转为 JSON 字符串
- * @param bean
- * @return
- */
- public static <T> void output(T bean, OutputStream out) throws IOException {
- IOUtils.copy(new StringReader(convertString(bean)), out);
- }
- /**
- * 把 Java Bean 转为 JSON 字符串
- * @param bean
- * @return
- */
- public static void output(Object bean, Writer out) throws IOException {
- IOUtils.copy(new StringReader(convertString(bean)), out);
- }
- /**
- * 把 Java Bean 转为 JSON 字符串,并从 HttpServletResponse 输出
- * @param bean
- * @return
- */
- public static void output(Object bean,
- HttpServletResponse response) {
- output(bean, response, "UTF-8");
- }
- /**
- * 把 Java Bean 转为 JSON 字符串,并从 HttpServletResponse 输出
- * @param bean
- * @return
- */
- public static void output(Object bean,
- HttpServletResponse response,
- String charset) {
- response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString());
- response.setCharacterEncoding(charset);
- try {
- IOUtils.copy(new StringReader(convertString(bean)), response.getWriter());
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- }
- /**
- * 把 Rest API 中提交的 JSON 表单数据,转为 clazz 指定的 Bean 类型的 LIST 集合
- * @param request HttpRestAPI
- * * @param clazz JSON 能转换的 Class
- * @return
- */
- public static <T> List<T> beanArray(HttpServletRequest request,
- Class<T> clazz) {
- String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
- try {
- String json = StringUtils.trim(IOUtils.toString(request.getReader()));
- // Rest API 接口
- if(json.startsWith("[") && json.endsWith("]")) {
- return JSONObject.parseArray(json, clazz);
- }
- throw new IllegalArgumentException("错误的数组消息实体。" + json);
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- }
- /**
- * 把 Rest API 中提交的 JSON 表单数据,转为 clazz 指定的 Bean 类型
- * @param request HttpRestAPI
- * @param clazz JSON 能转换的 Class
- * @return
- */
- public static <T> T beanObject(HttpServletRequest request,
- Class<T> clazz) {
- String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
- try {
- String json = StringUtils.trim(IOUtils.toString(request.getReader()));
- // Rest API 接口
- if(json.startsWith("{") && json.endsWith("}")) {
- return JSONObject.parseObject(json, clazz);
- }
- throw new IllegalArgumentException("错误的消息对象实体。" + json);
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- }
- /**
- * 把 Rest API 中提交的 JSON 表单数据,转为 JSON 类型
- * @param request HttpRestAPI
- * @return
- */
- public static JSON jsonObject(HttpServletRequest request) {
- String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
- try {
- String json = StringUtils.trim(IOUtils.toString(request.getReader()));
- // Rest API 接口
- if(json.startsWith("{") && json.endsWith("}")) {
- return JSONObject.parseObject(json);
- } else if(json.startsWith("[") && json.endsWith("]")) {
- return JSON.parseArray(json);
- }
- throw new IllegalArgumentException("错误的消息对象实体。" + json);
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- }
- }
|