目前工作需要做一个接口:前端要传一个json给后台,后台要拿到这个json去调用第三方接口。
问题是第三方所需的json参数和前端传给我的json里面字段名称不同。
我想了一种方案:SpringMVC直接用对象A来接收前端传我的参数,然后把对象A和对象B里的属性值进行一个copy,再把对象B转成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请求
//。。。。。
}
}
如果有更好的方法希望大神指点