通过反射将request内带的参数直接映射到实体类上

package com.example.server.util;

import com.example.server.util.TypeUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class ObjectUtil {
    public static Object getObjectByRequest(Class<?> clazz, HttpServletRequest request) throws ClassNotFoundException, ParameterError {
        Object entity = null;
        try{
            entity = clazz.getDeclaredConstructor().newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                String value = request.getParameter(field.getName());
                if(value==null){
                    continue;
                }
                String filedName = field.getName();
                Class<?> filedType = field.getType();
                String setterName = "set"+ StringUtils.capitalize(filedName);
                Method setterMethod;
                try {
                    setterMethod = clazz.getMethod(setterName,filedType);
                } catch (NoSuchMethodException e) {
                    setterMethod = null;
                    e.printStackTrace();
                }
                if(setterMethod==null){
                    continue;
                }
                setterMethod.invoke(entity, TypeUtils.stringToTarget(value,filedType));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ParameterError("request参数错误,无法获得对应实例类");
        }
        return entity;
    }

    public static class ParameterError extends Exception{
        private String message;
        public ParameterError(String message){
            super(message);
            this.message = message;
        }
    }
}

使用:

User user = (User) ObjectUtil.getObjectByRequest(User.class,request);

User:

@Data
@TableName("s_user")
public class User{
    public static final String ROLE_STUDENT = "student";
    public static final String ROLE_TEACHER = "teacher";
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;

    private String loginName;
    private String password;
    private String role;
    private String name;
}

Url(post也可以)

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Java中,可以通过以下方式将request.getParameterMap()获取到的参数映射实体: 1. 首先,定义一个实体类,包含需要映射参数字段和对应的setter方法: ``` public class User { private String username; private String password; public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } } ``` 2. 然后,通过request.getParameterMap()方法获取到参数Map,遍历Map,并通过反射参数值设置到实体对象中: ``` Map<String, String[]> paramMap = request.getParameterMap(); User user = new User(); for (Map.Entry<String, String[]> entry : paramMap.entrySet()) { String paramName = entry.getKey(); String[] paramValues = entry.getValue(); try { Field field = user.getClass().getDeclaredField(paramName); field.setAccessible(true); field.set(user, paramValues[0]); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } ``` 上述代码中,首先通过request.getParameterMap()方法获取到参数Map,然后遍历Map,获取每个参数的名称和值。接着,通过实体类的反射机制,获取对应的字段,并将参数值设置到实体对象中。 需要注意的是,如果参数Map中包含的参数名称与实体类中的字段名称不一致,需要进行对应的处理。例如,将参数名称转换成对应的字段名称。另外,如果实体类中包含的字段较多,可以考虑使用Java Bean Utils库进行参数映射
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值