推土机将地图复制到另一个地图

时间:2015-06-25 17:41:47

标签: java dozer

我已经尝试将Map(String,ProposalViewBean)复制到另一个Map(String,Proposal)

我创建了一个自定义转换器:

public class ProposalsDozerConverter  extends DozerConverter <HashMap<String,Proposal>, HashMap<String,ProposalViewBean>> {

    @Resource(name="mapper")
    private Mapper mapper;


    public ProposalsDozerConverter(Class<HashMap<String, Proposal>> prototypeA,
                                                      Class<HashMap<String, ProposalViewBean>> prototypeB) {

        super(prototypeA, prototypeB);

    }

    @Override
    public HashMap<String, Proposal> convertFrom( HashMap<String, ProposalViewBean> proposalsViewBean, HashMap<String, Proposal> proposals) {

        System.out.println("convertFrom");

        /*if (proposals == null){
            proposals = new TreeHashMap<String, Proposal>();
        }*/

        for (Map.Entry<String, ProposalViewBean> entry : proposalsViewBean.entrySet()){
            Proposal p = mapper.map(entry.getValue(), Proposal.class);
            proposals.put(entry.getKey(), p);
        }

        return proposals;
    }

    @Override
    public HashMap<String, ProposalViewBean> convertTo(HashMap<String, Proposal> proposals, HashMap<String, ProposalViewBean> proposalsViewBean) {

        System.out.println("convertTo");

        /*if (proposalsViewBean == null){
            proposalsViewBean = new TreeMap<String, ProposalViewBean>();
        }*/

        for (Map.Entry<String, Proposal> entry : proposals.entrySet()){
            ProposalViewBean p = mapper.map(entry.getValue(), ProposalViewBean.class);
            proposalsViewBean.put(entry.getKey(), p);
        }

        return proposalsViewBean;
    }

然后我用XML配置:

<configuration>
    <custom-converters>
        <converter type="com.hsbc.hbbr.frb.converters.ProposalsDozerConverter">
            <class-a>java.util.HashMap</class-a>
            <class-b>java.util.HashMap</class-b>
        </converter>
    </custom-converters>
</configuration>

<mapping>
    <class-a>com.hsbc.hbbr.frb.viewbean.ConsumerLoanViewBean</class-a>
    <class-b>com.hsbc.hbbr.frb.model.ConsumerLoan</class-b>
    <field custom-converter="com.hsbc.hbbr.frb.converters.ProposalsDozerConverter">
        <a>proposals</a>
        <b>proposals</b>
        <a-hint>com.hsbc.hbbr.frb.viewbean.ProposalViewBean</a-hint>
        <b-hint>com.hsbc.hbbr.frb.model.Proposal</b-hint>
    </field>
</mapping>

当我尝试转换时,我发现了这个错误:

Caused by: java.lang.InstantiationException: com.hsbc.hbbr.frb.converters.ProposalsDozerConverter
at java.lang.Class.newInstance(Class.java:359)

我不知道如何解决这个问题。有人遇到过这个问题吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我的代码和XML存在一些错误。 首先,我需要更改映射:

<mapping>
        <class-a>com.hsbc.hbbr.frb.viewbean.ConsumerLoanViewBean</class-a>
        <class-b>com.hsbc.hbbr.frb.model.ConsumerLoan</class-b>
        <field custom-converter="com.hsbc.hbbr.frb.converters.ProposalsDozerConverter">
            <a>proposals</a>
            <b>proposals</b>
            <a-hint>java.util.HashMap</a-hint>
            <b-hint>java.util.HashMap</b-hint>
        </field>
    </mapping>

我的意图是将Map复制到另一个Map,而不是复制Map中的Object。

检查完转换器后,我意识到弹簧没有发送Mapper实例:

@Resource(name="mapper")
private Mapper mapper;

这就是异常的原因。要获取Mapper的实例,我需要实现Interface MapperAware.Implement这个Interface Dozer注入一个Mapper实例。 我不得不改变类的构造函数:

public ProposalsDozerConverter() {
        super( ((Class<HashMap<String,Proposal>>) (Class<?>) HashMap.class), (Class<HashMap<String,ProposalViewBean>>) (Class<?>) HashMap.class  );
    } 

因为原始类型将在运行时擦除。因此Dozer不知道Map内部是什么类型的对象。 我的最终代码是:

public class ProposalsDozerConverter  extends DozerConverter <HashMap<String,Proposal>, HashMap<String,ProposalViewBean>>
                                                                                                implements MapperAware {
    private Mapper mapper;

    public ProposalsDozerConverter() {
        super( ((Class<HashMap<String,Proposal>>) (Class<?>) HashMap.class), (Class<HashMap<String,ProposalViewBean>>) (Class<?>) HashMap.class  );
    }

    @Override
    public HashMap<String, Proposal> convertFrom( HashMap<String, ProposalViewBean> proposalsViewBean, HashMap<String, Proposal> proposals) {

        for (Map.Entry<String, ProposalViewBean> entry : proposalsViewBean.entrySet()){
            Proposal p = mapper.map(entry.getValue(), Proposal.class);
            proposals.put(entry.getKey(), p);
        }

        return proposals;
    }

    @Override
    public HashMap<String, ProposalViewBean> convertTo(HashMap<String, Proposal> proposals, HashMap<String, ProposalViewBean> proposalsViewBean) {

        for (Map.Entry<String, Proposal> entry : proposals.entrySet()){
            ProposalViewBean p = mapper.map(entry.getValue(), ProposalViewBean.class);
            proposalsViewBean.put(entry.getKey(), p);
        }

        return proposalsViewBean;
    }

    @Override
    public void setMapper(Mapper mapper ) {
        this.mapper = mapper;
    }
}