推土机复制地图到另一个地图
Dozer copy Map to another Map
我一直在尝试将 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)
我不知道要解决这个问题。有人遇到过这个问题吗?
我找到了问题的解决方案。我的代码和 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 中的对象。
检查转换器后,我意识到 spring 没有发送 Mapper 的实例:
@Resource(name="mapper")
private Mapper mapper;
这就是异常的原因。为了获得 Mapper 的实例,我需要实现接口 MapperAware.Implement 这个接口 Dozer 注入了一个 Mapper 实例。
我不得不更改 class:
的构造函数
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;
}
}
我一直在尝试将 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)
我不知道要解决这个问题。有人遇到过这个问题吗?
我找到了问题的解决方案。我的代码和 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 中的对象。
检查转换器后,我意识到 spring 没有发送 Mapper 的实例:
@Resource(name="mapper")
private Mapper mapper;
这就是异常的原因。为了获得 Mapper 的实例,我需要实现接口 MapperAware.Implement 这个接口 Dozer 注入了一个 Mapper 实例。 我不得不更改 class:
的构造函数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;
}
}