Re: Map JAXB generated java beans to DTO's

410 views
Skip to first unread message
Message has been deleted

Sjaak Derksen

unread,
May 11, 2016, 4:21:36 PM5/11/16
to mapstruct-users


On Wednesday, May 4, 2016 at 4:53:25 PM UTC+2, Enes Sejfi wrote:

I'm trying to use Mapstruct to map JAXB generated java beans to DTO's.

This are my classes

Generated JAXB class :

@XmlRootElement(name = "root")
public class Root {

    @XmlElement(required = true)
    protected Root.CartItems cartItems;


    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "element"
    })
    public static class CartItems {

        protected List<Root.CartItems.Element> element;

        public List<Root.CartItems.Element> getElement() {
            if (element == null) {
                element = new ArrayList<Root.CartItems.Element>();
            }
            return this.element;
        }

        public static class Element {

            protected String productId;

            // Setter and Getter ...
        }

    }
}


DTO class :

public class ShoppingCart implements Serializable {

    private List<CartItem> cartItems;

}


Using the following mapping strategy I got the error message :

@Mapper
public abstract class TestMapper {

    abstract ShoppingCart mapRootToShoppingCart(Root root);

    abstract List<CartItem> mapRootShopperToShopper(CartItems e);
}

==>
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) : Compilation failure:
[ERROR] TestMapper.java:[24,70] Can't generate mapping method from non-iterable type to iterable type.
[ERROR] TestMapper.java:[16,27] Can't map property "Root.CartItems cartItems" to "java.util.List cartItems". Consider to declare/implement a mapping method: "java.util.List map(Root.CartItems value)".


MapStruct tells you it does not know how to map a single element to a list. You need to implement:

List<CartItem> mapRootShopperToShopper(CartItems e).

E.g.

List<CartItem> mapRootShopperToShopper(CartItems e) {
     return Arrays.asList(e);
}

This solution also does not work.

@Mapper
public abstract class TestMapper {

    abstract ShoppingCart mapRootToShoppingCart(Root root);

    @Mapping(target = "cartItems", source = "cartItems.element")
    abstract List<CartItem> mapRootShopperToShopper(List<Root.CartItems.Element> e);
}

==>
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) : Compilation failure:
[ERROR] TestMapper.java:[25,5] No property named "cartItems.element" exists in source parameter(s).
[ERROR] TestMapper.java:[16,27] Can't map property "Root.CartItems cartItems" to "java.util.List cartItems". Consider to declare/implement a mapping method: "java.util.List map(Root.CartItems value)".
[ERROR] TestMapper.java:[26,70] No implementation can be generated for this method. Found no method nor implicit conversion for mapping source element type into target element type.


MapStruct generates automatically a list to list method for you. But you still need to tell it how to map an element of a list to an element of the other list. So

@Mapping
 abstract CartItem mapRootShopperToShopper(Root.CartItems.Element e);

or implement a HandWritten method.

Does anyone has a solution how to map a class that contains a List (Root) to java.util.List<CartItem>


Best regards,
Sjaak
Reply all
Reply to author
Forward
0 new messages