dozer Boolean property mapping
Asked Answered
C

4

11

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.

Cointon answered 27/4, 2011 at 10:44 Comment(3)
Are you using JAXB to generate your DTOs, by any chance?Thetisa
yes I am, is there some way you can make it generate get accessors?Brickwork
note that Dozer only behaves this way when the is method has type Boolean - it maps boolean isFoo() methods fine.Astrobiology
T
11

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. :)

Termitarium answered 25/5, 2012 at 14:33 Comment(0)
A
7

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

Asepsis answered 27/4, 2011 at 11:57 Comment(0)
T
3

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

Thetisa answered 27/4, 2011 at 12:7 Comment(5)
Thanks, any idea where I can find this plugin (the link above doesn't work) and how I use it with the ant wsimport task?Brickwork
Sorry, messed the link up. Fixed now.Thetisa
I'm afraid I was using it with MavenThetisa
I figured out how to integrate it with Ant, but it failed with this error [java] java.util.ServiceConfigurationError: com.sun.tools.xjc.Plugin: Provider org.jvnet.jaxb2_commons.BooleanGetter could not be instantiated: java.lang.ClassCastExceptionBrickwork
Looks like an ant config issue... can you get any more of the stack trace? Does it work if you use the command line xjc?Thetisa
D
0

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>
Dredge answered 5/6, 2013 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.