dozer 布尔属性映射
如果布尔属性的访问器定义为 isProperty() 而不是 getProperty(),Dozer 似乎不会映射该属性。
以下 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()
,它就会通过。
我可以在 Dozer 映射文件中设置一个标志/选项来指示它使用 is
或 get
访问器来获取布尔属性吗?或者,我可以为每个布尔属性添加自定义规则,但这不是很有吸引力。
尽管上面的示例是用 Groovy 编写的,但我没有理由相信等效的 Java 代码不会表现出相同的行为。
这些 DTO 由 JAXB 生成(它生成“is”访问器,而不是布尔值的“get”访问器),因此我无法重命名访问器。我正在使用推土机 5.3.2。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许您可以使用自定义 getter 方法来使用它。
这是示例映射(将其写入 dozer-mapping 文件),
因此现在 dozer 将使用 isFoo 而不是预定义的 getFoo。
希望这对你有用。 :)
May be you can use custom getter method to use it.
here s the example mapping (Write it in dozer-mapping file)
So now dozer will use isFoo instead of predefined getFoo.
Hope this works for you. :)
为布尔包装类生成“is”方法是 JAXB 中的一个错误,请参阅 Java Bean、BeanUtils 和布尔包装类 和 http://java.net/jira/browse/JAXB-131 了解详细信息。似乎已在 jaxb 2.1.13 中修复
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. Seems to be fixed in jaxb 2.1.13
这是 JAXB 中的一个错误,小 b
boolean
应该有isFoo()
。您可以在更高版本的 JAXB 中使用 -enableIntrospection 选项,或者使用旧的布尔 getter xjc 插件 http://fisheye5.cenqua.com/browse/~raw,r=MAIN/jaxb2-commons/www/boolean-getter/index.htmlThis is a bug in JAXB, the small-b
boolean
should haveisFoo()
. 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还有另一种方法可以实现正确的推土机映射(我认为最干净的方法):
或者前面已经提到的方法:
There also is another way of achieving the correct dozer mapping (the cleanest in my opinion):
OR the way already mentioned earlier: