为不同包中的对象创建通用转换器

发布于 2024-12-06 20:25:17 字数 791 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自在安然 2024-12-13 20:25:17

您可以为此使用反射。最简单、最干净的方法可能是使用 Apache Common BeanUtils,或者 PropertyUtils#copyPropertiesBeanUtils#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.

浪推晚风 2024-12-13 20:25:17

您还可以查看 Dozer

You can also check out Dozer

不再让梦枯萎 2024-12-13 20:25:17

我认为你应该考虑使用反射

I think you should consider using reflection.

绮筵 2024-12-13 20:25:17

使用 commons beanutils 库,您可以执行此实用程序类:

public class BeanUtilCopy {
    private static BeanUtilsBean beanUtilsBean;
    private static ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
    static {                   
        convertUtilsBean.register(new Converter() { //2
            public <T> T convert(Class<T> type, Object value) {
                T dest = null;
                try {
                    dest = type.newInstance();
                    BeanUtils.copyProperties(dest, value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return dest;
            }
        }, Wheel.class);
    beanUtilsBean = new BeanUtilsBean(convertUtilsBean);
}

    public static void copyBean(Object dest, Object orig) throws Exception {
        beanUtilsBean.copyProperties(dest, orig); //1
    }

当 (1) beanUtilsBean 使用转换器时 (2) 将 Wheel**X** 值传递到目标 bean 中的 Wheel。

使用示例:

    CarB carB = new CarB();
    carB.setName("car B name");
    carB.setWeight(115);
    WheelB wheelB = new WheelB();
    wheelB.setName("wheel B name");
    wheelB.setType(05);

    carB.setWheel(wheelB);

    Car car1 = new Car();
    BeanUtilCopy.copyBean(car1, carB);

    System.out.println(car1.getName());
    System.out.println(car1.getWeight());
    System.out.println(car1.getWheel().getName());
    System.out.println(car1.getWheel().getType());

输出:

汽车 B 名称

115

车轮 B 名称

5

Using commons beanutils library you may do this utility class:

public class BeanUtilCopy {
    private static BeanUtilsBean beanUtilsBean;
    private static ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
    static {                   
        convertUtilsBean.register(new Converter() { //2
            public <T> T convert(Class<T> type, Object value) {
                T dest = null;
                try {
                    dest = type.newInstance();
                    BeanUtils.copyProperties(dest, value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return dest;
            }
        }, Wheel.class);
    beanUtilsBean = new BeanUtilsBean(convertUtilsBean);
}

    public static void copyBean(Object dest, Object orig) throws Exception {
        beanUtilsBean.copyProperties(dest, orig); //1
    }

When (1) beanUtilsBean use the converter (2) to pass the Wheel**X** values to the Wheel in destination bean.

Use sample:

    CarB carB = new CarB();
    carB.setName("car B name");
    carB.setWeight(115);
    WheelB wheelB = new WheelB();
    wheelB.setName("wheel B name");
    wheelB.setType(05);

    carB.setWheel(wheelB);

    Car car1 = new Car();
    BeanUtilCopy.copyBean(car1, carB);

    System.out.println(car1.getName());
    System.out.println(car1.getWeight());
    System.out.println(car1.getWheel().getName());
    System.out.println(car1.getWheel().getType());

The output:

car B name

115

wheel B name

5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文