我以爲春天框架可能help.Here是我的春天核心4.1.5的例子:
類聲明:
public static class A{
private String a;
private String b;
private String c;
private C d;
private int e;
//getter&setter here
public static class C {
private String aa;
private String bb;
private String cc;
//getter&setter here
public static class B{
private Integer a;
private Long b;
private Boolean c;
private D d;
private Integer e;
//getter&setter here
public static class D {
private Integer aa;
private Long bb;
private Boolean cc;
//getter&setter here
class ObjectConverter implements Converter<Object, Object> {
private final ConversionService conversionService;
private final Class targetClass;
public ObjectConverter(ConversionService conversionService, Class targetClass) {
this.conversionService = conversionService;
this.targetClass = targetClass;
}
@Override
public Object convert(Object source) {
Object ret=null;
try {
ret = targetClass.newInstance();
Field[] fields = targetClass.getDeclaredFields();
for (Field field : fields) {
PropertyDescriptor sourceDescriptor = new PropertyDescriptor(field.getName(),source.getClass());
PropertyDescriptor targetDescriptor = new PropertyDescriptor(field.getName(),targetClass);
if (sourceDescriptor.getReadMethod()==null || targetDescriptor.getWriteMethod() == null) {
//record error here
break;
}
Class<?> sourcePType = sourceDescriptor.getPropertyType();
Class<?> targetPType = targetDescriptor.getPropertyType();
if(conversionService.canConvert(sourcePType,targetPType)){
Object sourceValue = sourceDescriptor.getReadMethod().invoke(source);
Object targetValue = conversionService.convert(sourceValue, targetPType);
targetDescriptor.getWriteMethod().invoke(ret,targetValue);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}
主要方法:
A orgA = new A();
orgA.a="123";
orgA.b="1234567";
orgA.c="1";
orgA.d = new C();
orgA.d.aa="321";
orgA.d.bb="7654321";
orgA.d.cc="0";
orgA.e=-99;
System.out.println(orgA);
try {
ConfigurableConversionService conversionService = new DefaultConversionService();
//if you are not satisfied with the final A result, then
//customize your own Boolean->String Converter;
// conversionService.addConverter(new Converter<Boolean, String>() {
// @Override
// public String convert(Boolean source) {
// return source==null||!source?"0":"1";
// }
// });
conversionService.addConverter(A.class, B.class, new ObjectConverter(conversionService, B.class));
conversionService.addConverter(B.class, A.class, new ObjectConverter(conversionService, A.class));
conversionService.addConverter(C.class, D.class, new ObjectConverter(conversionService, D.class));
conversionService.addConverter(D.class, C.class, new ObjectConverter(conversionService, C.class));
B b = conversionService.convert(orgA, B.class);
System.out.println(b);
A a = conversionService.convert(b, A.class);
System.out.println(a);
} catch (Exception e) {
e.printStackTrace();
}
結果:
=
A ='123',b ='1234567',c ='1',d = C {aa ='321',bb ='7654321',cc ='0'},e = -99}
A {a ='123',b = 123},b = 1234567,c =真,d = D {aa = 321,bb = 7654321,cc =假},e = -99}
'1234567',c ='true',d = C {aa ='321',bb ='7654321',cc ='false'},e = -99}
這似乎很好的解決方案,但我有些問題。爲什麼我需要聲明轉換器,它應該自動地自動查找源和目標類。 – SacJn
我已經使用struts框架了,所以如果我使用這個庫,它會引起一些衝突嗎?我現在發現的另一個問題。某些字段的源和轉換器類不具有相同的名稱。 – SacJn