Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dozer Boolean property mapping

Tags:

java

groovy

dozer

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.

like image 802
Dónal Avatar asked Apr 27 '11 10:04

Dónal


People also ask

What is Dozer mapping?

Overview. Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another, attribute by attribute. The library not only supports mapping between attribute names of Java Beans, but also automatically converts between types – if they're different.

What is bean mapping?

It is mainly bean to bean mapper that recursively copies data from one java object to another java object – attribute by attribute. We realize it's full capability when We are dealing with deeply-nested complex java beans, which we are easily seen in large enterprise applications.


3 Answers

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

like image 199
Priyank Doshi Avatar answered Nov 07 '22 08:11

Priyank Doshi


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

like image 40
Jörn Horstmann Avatar answered Nov 07 '22 08:11

Jörn Horstmann


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

like image 38
artbristol Avatar answered Nov 07 '22 08:11

artbristol