为不同包中的对象创建通用转换器
我有 5 个 Web 服务,A、B、C、D 和 E。每个服务都有自动生成的对象,其结构完全相同,但名称不同且位于不同的包中。
com.ws.a.carA contains parameters and com.ws.a.wheelA
com.ws.b.carB contains parameters and com.ws.b.wheelB
com.ws.c.carC contains parameters and com.ws.c.wheelC
com.ws.d.carD contains parameters and com.ws.d.wheelD
com.ws.e.carE contains parameters and com.ws.e.wheelE
我想创建一个函数,可以将每个对象(和内轮)转换为名为的对象,
com.model.car,
但我不想要很多函数,例如:
com.model.car convert(com.ws.a.objA obj)
com.model.car convert(com.ws.b.objB obj)
...
问题是,我可以' t 为所有对象提供一个要实现的通用接口,因为我不想手动更改自动生成的类(它们经常重新创建)。
我需要一种方法(可能使用泛型)来创建
com.model.car convert(T obj)
适用于所有汽车类型的通用函数,但我不确定如何实现它。
I have 5 webservices, A, B, C, D, and E. Each has autogenerated objects of the exact same structure, but with different names and in different packages.
com.ws.a.carA contains parameters and com.ws.a.wheelA
com.ws.b.carB contains parameters and com.ws.b.wheelB
com.ws.c.carC contains parameters and com.ws.c.wheelC
com.ws.d.carD contains parameters and com.ws.d.wheelD
com.ws.e.carE contains parameters and com.ws.e.wheelE
I want to create one function that can convert each of these objects (and the inner wheel) to a object named
com.model.car,
but I dont wan't many functions like :
com.model.car convert(com.ws.a.objA obj)
com.model.car convert(com.ws.b.objB obj)
...
The problem is, I can't give all the objects a common interface to implement, because I don't want to manually change the autogenerated classes (they are recreated frequently).
I need a way, probably with generics, to create a common function
com.model.car convert(T obj)
that will work for all the car types but I'm not sure how to implement it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以为此使用反射。最简单、最干净的方法可能是使用 Apache Common BeanUtils,或者 PropertyUtils#copyProperties 或 BeanUtils#copyProperties。
PropertyUtils#copyProperties 将值从一个对象复制到另一个对象,其中字段名称相同。因此,使用 copyProperties(dest, orig),它会为两个对象中存在的所有字段调用 dest.setFoo(orig.getFoo()) 。
BeanUtils#copyProperties 执行相同的操作,但您可以注册转换器,以便在必要时将值从 String 转换为 Int。有许多标准转换器,但您可以注册自己的转换器,在您的情况下为 com.ws.a.wheelA 到 com.model.wheel,或其他。
You can use reflection for this. The easiest and cleanest way would probably be to use Apache Common BeanUtils, either PropertyUtils#copyProperties or BeanUtils#copyProperties.
PropertyUtils#copyProperties copies the values from one object to another, where the field names are the same. So with copyProperties(dest, orig), it calls dest.setFoo(orig.getFoo()) for all fields which exist in both objects.
BeanUtils#copyProperties does the same, but you can register converters so that the values get converted from String to Int, if necessary. There are a number of standard converters, but you can register your own, in your case com.ws.a.wheelA to com.model.wheel, or whatever.
您还可以查看 Dozer
You can also check out Dozer
我认为你应该考虑使用反射。
I think you should consider using reflection.
使用 commons beanutils 库,您可以执行此实用程序类:
当 (1) beanUtilsBean 使用转换器时 (2) 将 Wheel**X** 值传递到目标 bean 中的 Wheel。
使用示例:
输出:
汽车 B 名称
115
车轮 B 名称
5
Using commons beanutils library you may do this utility class:
When (1) beanUtilsBean use the converter (2) to pass the Wheel**X** values to the Wheel in destination bean.
Use sample:
The output:
car B name
115
wheel B name
5