java 推土机布尔属性映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5802797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dozer Boolean property mapping
提问by Dónal
It appears that Dozer will not map a Boolean property if the accessor of that property is defined as isProperty()
rather than getProperty()
.
如果该属性的访问器定义为isProperty()
而不是,则 Dozer 似乎不会映射 Boolean 属性getProperty()
。
The following groovy script illustrates the problem:
下面的 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
The assertion on the final line fails. However, if I rename ProductDto.isFoo()
to ProductDto.getFoo()
it passes.
最后一行的断言失败。不过,如果我重新命名ProductDto.isFoo()
,以ProductDto.getFoo()
它传递。
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.
是否有我可以在 Dozer 映射文件中设置的标志/选项来指示它对布尔属性使用is
orget
访问器?或者,我可以为每个布尔属性添加自定义规则,但这不是很吸引人。
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.
尽管上面的示例是用 Groovy 编写的,但我没有理由相信等效的 Java 代码不会表现出相同的行为。
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.
这些 DTO 是由 JAXB 生成的(它生成一个“is”访问器,而不是布尔值的“get”访问器),所以我不能重命名这些访问器。我正在使用推土机 5.3.2。
回答by Priyank Doshi
May be you can use custom getter method to use it.
可能您可以使用自定义 getter 方法来使用它。
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. :)
所以现在推土机将使用 isFoo 而不是预定义的 getFoo。希望这对你有用。:)
回答by J?rn Horstmann
Generating "is" methods for the Boolean wrapper class is a bug in JAXB, see Java Beans, BeanUtils, and the Boolean wrapper classand http://java.net/jira/browse/JAXB-131for details. Seems to be fixed in jaxb 2.1.13
为布尔包装类生成“is”方法是 JAXB 中的一个错误,有关详细信息,请参阅Java Beans、BeanUtils 和布尔包装类和http://java.net/jira/browse/JAXB-131。似乎已在 jaxb 2.1.13 中修复
回答by artbristol
This is a bug in JAXB, the small-b boolean
should have 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
这是 JAXB 中的一个错误,small-bboolean
应该有isFoo()
. 您可以在更高版本的 JAXB 中使用 -enableIntrospection 选项,或者使用旧的布尔 getter xjc 插件http://fisheye5.cenqua.com/browse/~raw,r=MAIN/jaxb2-commons/www/boolean-getter /index.html
回答by Mathias G.
There also is another way of achieving the correct dozer mapping (the cleanest in my opinion):
还有另一种方法可以实现正确的推土机映射(我认为是最干净的):
<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>
OR the way already mentioned earlier:
或前面已经提到的方式:
<mapping>
<class-a>ProductDto</class-a>
<class-b>ProductCommand</class-b>
<field>
<a get-method=”isFoo”>foo</a>
<b>foo</b>
</field>
</mapping>