request转换为java bean

request body 转换成 java bean

  /**
     * 将requestBody解析为对应的bean
     *
     * @param request
     * @param clazz
     * @param <T>
     * @return
     * @throws IOException
     */
    public static <T> T requestBody2Bean(HttpServletRequest request, Class<? extends T> clazz) {
        try {
            return JSONObject.parseObject(read(request.getReader()), clazz);
        } catch (IOException e) {
            logger.error(e);
            return null;
        }

    }
    /**
     * @param reader
     * @return
     */
    private static String read(Reader reader) {
        int bufferSize = 1024 * 8;
        char[] buf = new char[bufferSize];
        try (StringWriter writer = new StringWriter()) {
            int read;
            while ((read = reader.read(buf)) != -1) {
                writer.write(buf, 0, read);
            }
            return writer.getBuffer().toString();
        } catch (IOException e) {
            logger.error(e);
            return "";
        }
    }

request param 装换成 java bean

/**
     * <p>按指定字符集解析HttpServletRequest 生成给定的类的Bean</p>
     * <p>
     * 1.字符集不支持,获取bean类型内省信息失败时,返回null
     * 2.生成bean属性失败时,属性值不设置
     * </p>
     *
     * @param request
     * @param t
     * @param charset
     * @param <T>
     * @return
     */
    public static <T> T convertHttpRequest2Bean(HttpServletRequest request, T t, String charset) {
        BeanInfo beanInfo;
        if (null == request) return null;
        try {
            request.setCharacterEncoding(charset);
            beanInfo = Introspector.getBeanInfo(t.getClass());
        } catch (Exception e) {
            logger.error(e);
            return null;
        }

        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();//bean中全部属性
        PropertyDescriptor descriptor;//bean属性描述
        String propertyName;//bean属性名称
        Class fieldType;//bean属性类别
        String paraValueStr;//request对应的属性值字符串
        Object paraValueObj;//request转换为对应类别的属性值
        for (int i = 0; i < propertyDescriptors.length; i++) {
            descriptor = propertyDescriptors[i];
            propertyName = descriptor.getName();
            fieldType = descriptor.getPropertyType();
            paraValueStr = request.getParameter(propertyName);
            if (StringUtils.isEmpty(paraValueStr)) continue;
            try {
                if (long.class.equals(fieldType)) {
                    paraValueObj = Long.parseLong(paraValueStr.trim());
                } else if (short.class.equals(fieldType)) {
                    paraValueObj = Short.parseShort(paraValueStr.trim());
                } else if (int.class.equals(fieldType)) {
                    paraValueObj = Integer.parseInt(paraValueStr.trim().replaceAll("^(-?\\d+)\\.[0]+$", "$1"));
                } else {
                    paraValueObj = paraValueStr;
                }
                descriptor.getWriteMethod().invoke(t, paraValueObj);
            } catch (Exception e) {
                logger.error(e);
            }
        }
        return t;
    }

 

posted @ 2019-07-13 14:02  斯然  阅读(906)  评论(0编辑  收藏  举报