繁体   English   中英

推土机 Boolean 属性映射

[英]dozer Boolean property mapping

如果该属性的访问器被定义为isProperty()而不是getProperty() ,那么 Dozer 似乎不会 map 一个 Boolean 属性。

以下 groovy 脚本说明了该问题:

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

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

我可以在推土机映射文件中设置一个标志/选项来指示它使用isget访问器来获取 boolean 属性吗? 或者,我可以为每个 boolean 属性添加自定义规则,但这不是很吸引人。

尽管上面的示例是用 Groovy 编写的,但我没有理由相信等效的 Java 代码不会表现出相同的行为。

这些 DTO 由 JAXB 生成(它生成一个“is”访问器,而不是布尔值的“get”访问器),所以我不能重命名访问器。 我正在使用推土机 5.3.2。

也许您可以使用自定义 getter 方法来使用它。

这是示例映射(将其写入推土机映射文件)

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

所以现在 dozer 将使用 isFoo 而不是预定义的 getFoo。 希望这对你有用。 :)

Generating "is" methods for the Boolean wrapper class is a bug in JAXB, see Java Beans, BeanUtils, and the Boolean wrapper class and http://java.net/jira/browse/JAXB-131 for details. 似乎已在 jaxb 2.1.13 中修复

这是 JAXB 中的一个错误,small-b boolean应该有isFoo() You can either use the -enableIntrospection option with later versions of JAXB, or use the oldish boolean getter xjc plugin http://fisheye5.cenqua.com/browse/~raw,r=MAIN/jaxb2-commons/www/boolean-getter /index.html

还有另一种实现正确推土机映射的方法(我认为最干净):

<mapping>
    <class-a>ProductDto</class-a>
    <class-b>ProductCommand</class-b>
    <field>
       <a is-accessible=”true”>foo</a>
       <b is-accessible=”true”>foo</b>
    </field>
</mapping>

或者前面已经提到的方式:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM