dozer布尔属性映射 [英] dozer Boolean property mapping

查看:179
本文介绍了dozer布尔属性映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来,如果该属性的访问者定义为 isProperty()而不是 getProperty()<>,则Dozer不会映射布尔属性。 / code>。



下面的groovy脚本说明了这个问题:

  import org.dozer。* 

class ProductCommand {
布尔foo
}

公共类ProductDto {

私有布尔foo;

public Boolean isFoo(){this.foo}
public void setFoo(Boolean p0){this.foo = p0}
}

def mapper = new DozerBeanMapper()

dto = new ProductDto(foo:true)
assert dto.isFoo()

ProductCommand mappedCmd = mapper.map(dto, ProductCommand)
assert mappedCmd.foo

最后一行的断言失败。但是,如果我将 ProductDto.isFoo()重命名为 ProductDto.getFoo(),它就会通过。



是否有一个标志/选项可以在Dozer映射文件中设置,它将指示它使用 is 获取访问器的布尔属性?或者,我可以为每个布尔属性添加一个自定义规则,但这不是很吸引人。



虽然上面的例子是用Groovy编写的,但我没有理由这些DTO是由JAXB生成的(它生成一个is存取器,而不是一个get 布尔的访问器),所以我不能重命名访问器。我使用Dozer 5.3.2。

解决方案

可能你可以使用自定义getter方法来使用它。



这里是示例映射(写在推土机映射文件中)

 << ;映射> 
< class-a> ProductDto< / class-a>
< class-b> ProductCommand< / class-b>
< field>
< a get-method =isFoo> foo< / a>
< b> foo< / b>
< / field>
< / mapping>

现在推土机将使用isFoo代替预定义的getFoo。
希望这对你有用。 :)

It appears that Dozer will not map a Boolean property if the accessor of that property is defined as isProperty() rather than getProperty().

The following groovy script illustrates the problem:

import org.dozer.*

class ProductCommand {
    Boolean foo 
}

public class ProductDto  {

    private Boolean foo;        

    public Boolean isFoo() { this.foo }    
    public void setFoo(Boolean p0) { this.foo = p0 }           
}

def mapper =  new DozerBeanMapper()

dto = new ProductDto(foo: true)
assert dto.isFoo()

ProductCommand mappedCmd = mapper.map(dto, ProductCommand)
assert mappedCmd.foo

The assertion on the final line fails. However, if I rename ProductDto.isFoo() to ProductDto.getFoo() it passes.

Is there a flag/option I can set in the Dozer mapping file that will instruct it to use either an is or get accessor for boolean properties? Alternatively, I could add a custom rule for every boolean property, but this is not very appealing.

Although the example above is written in Groovy, I've no reason to believe the same behaviour wouldn't be exhibited by the equivalent Java code.

These DTOs are generated by JAXB (which generates an "is" accessor, rather than a "get" accessor for booleans), so I can't rename the accessors. I'm using Dozer 5.3.2.

解决方案

May be you can use custom getter method to use it.

here s the example mapping (Write it in dozer-mapping file)

<mapping>
  <class-a>ProductDto</class-a>
  <class-b>ProductCommand</class-b>
<field>
  <a get-method="isFoo">foo</a>
  <b>foo</b>
</field>
</mapping>

So now dozer will use isFoo instead of predefined getFoo. Hope this works for you. :)

这篇关于dozer布尔属性映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆