SpringMvc(2) : 请求参数反序列化

本文介绍了一种用于处理HTTP请求参数的实用方法,该方法能够将请求中的参数转换为指定类的对象,适用于请求转发和同一URL接口任意参数的情况。通过反序列化请求参数,可以方便地与业务对象进行映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

依赖类:

  https://blog.youkuaiyun.com/Lxinccode/article/details/82839955

  https://blog.youkuaiyun.com/Lxinccode/article/details/79414138

  https://blog.youkuaiyun.com/Lxinccode/article/details/79414399

使用场景 : 请求转发、同一 URL 接口任意参数

 

import com.aliyun.et.industry.device.api.enums.ResultCodeEnum;
import com.aliyun.et.industry.device.api.exception.DeviceException;
import com.aliyun.et.industry.templateboot.service.common.util.CheckCharacterCoding;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Auther: liyue
 * @Date: 2018/10/24 14:43
 * @Description:
 */
public class RequestUtil {

    /**
     * 请求参数反序列化
     * @param request
     * @param clazzT
     * @param <T>
     * @return
     */
    private static <T> T paramsHandleCenter(HttpServletRequest request, Class<T> clazzT) {
        List<String> attributeNames = ReflexUtil.getObjectAttributeNames(clazzT);
        StringBuffer json = new StringBuffer();
        json.append("{");
        for (String str : attributeNames) {
            String value = request.getParameter(str);
            if (value == null) {
                continue;
            }
            if (CheckCharacterCoding.checkISO(value)) {
                try {
                    value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
                } catch (Exception e) {
                }
            }
            if (value == null) {
                throw new DeviceException(ResultCodeEnum.ARGUMENT_ERROR);
            }
            json.append("\"").append(str).append("\":\"").append(value).append("\"").append(",");
        }
        json.delete(json.length() - 1, json.length());
        json.append("}");
        T t = JsonUtils.jsonToPojo(json.toString(), clazzT);
        return t;
    }
}

示例

请求 : http://127.0.0.1:8080/test?name=张三&age=14

首先得存在含有 name 与 age 的类并实现序列化接口,例如:

import java.io.Serializable;

/**
 * @Auther: liyue
 * @Date: 2018/10/24 14:55
 * @Description:
 */
public class User  implements Serializable {


    private static final long serialVersionUID = -2509731965245043361L;
    
    private String name;
    private Integer age;

    public User() {
    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

Controller


    @RequestMapping(value = "/test")
    public User test(HttpServletRequest httpServletRequest) {
        User user = RequestUtil.paramsHandleCenter(httpServletRequest, User.class);
        return user;
    }

 

END。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值