反射实现两个对象属性名不同之间的映射

目前工作需要做一个接口:前端要传一个json给后台,后台要拿到这个json去调用第三方接口。
问题是第三方所需的json参数和前端传给我的json里面字段名称不同。
我想了一种方案:SpringMVC直接用对象A来接收前端传我的参数,然后把对象A和对象B里的属性值进行一个copy,再把对象B转成json去调用第三方接口。
但是这种方法有个缺点:两个对象属性的顺序必须一致


public class userDto {
	private String name;
	private String password;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
public class UserInfo {
	private String user_name;
	private String user_password;
	private Integer user_age;
	
	
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public String getUser_password() {
		return user_password;
	}
	public void setUser_password(String user_password) {
		this.user_password = user_password;
	}
	public Integer getUser_age() {
		return user_age;
	}
	public void setUser_age(Integer user_age) {
		this.user_age = user_age;
	}
}
package test;  
  
import java.lang.reflect.Field;  
import java.lang.reflect.InvocationTargetException;  
import java.lang.reflect.Method;  
  
import com.alibaba.fastjson.JSONObject;  
  
public class TestORM {  
      
    public static  Object mapper(Object oldObj,Object newObj) throws InstantiationException, 
                        IllegalAccessException, IllegalArgumentException, InvocationTargetException, 
                        NoSuchMethodException, SecurityException{  
        Field[] oldField = oldObj.getClass().getDeclaredFields();   //源对象属性值集合  
        Field[] newField = newObj.getClass().getDeclaredFields();   //目标对象苏醒值集合  
          
        for(int i = 0; i < oldField.length; i++){  
             String oldname = oldField[i].getName();                //源对象属性名  
             oldname = oldname.substring(0, 1).toUpperCase() + oldname.substring(1);    //属性名第一个字符大写  
             Method oldMethod = oldObj.getClass().getMethod("get" + oldname);           //得到get方法  
             Object value =  oldMethod.invoke(oldObj);                                      //调用getter方法获取属性值    
               
             if (value != null) {    
                 Field f = newField[i];    
                 f.setAccessible(true);         //设置些属性是可以访问的    
                 f.set(newObj,value) ;          //给属性设值    
             }   
        }  
          
        return newObj;  
    }  
      
      
    public static void main(String [] args) throws InstantiationException, IllegalAccessException,
                                            IllegalArgumentException, InvocationTargetException,
                                            NoSuchMethodException, SecurityException{  
        //页面接收参数  
        UserDto dto = new UserDto();  
        dto.setName("zhangsan");  
        dto.setPassword("zhangsan123");  
          
        //初始化UserInfo  
        UserInfo userinfo = new UserInfo();   
        //进行字段之间的映射  
        mapper(dto, userinfo);  
          
        System.out.println("user_name:"+userinfo.getUser_name());  
        System.out.println("user_password:"+userinfo.getUser_password());  
        System.out.println(userinfo.getUser_age());  
        //转成json格式  
        JSONObject json = (JSONObject) JSONObject.toJSON(userinfo);  
        System.out.println(json);  
          
        //调用Http请求  
        //。。。。。  
          
    }  
}  

package test;

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

import com.alibaba.fastjson.JSONObject;

public class TestORM {
	
	public static  Object mapper(Object oldObj,Object newObj) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
        Field[] oldField = oldObj.getClass().getDeclaredFields();  	//源对象属性值集合
        Field[] newField = newObj.getClass().getDeclaredFields();	//目标对象苏醒值集合
        
        for(int i = 0; i < oldField.length; i++){
        	 String oldname = oldField[i].getName();				//源对象属性名
        	 oldname = oldname.substring(0, 1).toUpperCase() + oldname.substring(1); 	//属性名第一个字符大写
        	 Method oldMethod = oldObj.getClass().getMethod("get" + oldname);  			//得到get方法
             Object value =  oldMethod.invoke(oldObj);  									//调用getter方法获取属性值  
             
             if (value != null) {  
                 Field f = newField[i];  
                 f.setAccessible(true); 		//设置些属性是可以访问的  
                 f.set(newObj,value) ;        	//给属性设值  
             } 
        }
        
		return newObj;
	}
	
	
	public static void main(String [] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
		//页面接收参数
		UserDto dto = new UserDto();
		dto.setName("zhangsan");
		dto.setPassword("zhangsan123");
		
		//初始化UserInfo
		UserInfo userinfo = new UserInfo(); 
		//进行字段之间的映射
		mapper(dto, userinfo);
		
		System.out.println("user_name:"+userinfo.getUser_name());
		System.out.println("user_password:"+userinfo.getUser_password());
		System.out.println(userinfo.getUser_age());
		//转成json格式
		JSONObject json = (JSONObject) JSONObject.toJSON(userinfo);
		System.out.println(json);
		
		//调用Http请求
		//。。。。。
		
	}
}



如果有更好的方法希望大神指点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值