[英]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()
它就會通過。
我可以在推土機映射文件中設置一個標志/選項來指示它使用is
或get
訪問器來獲取 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.