Can't map property String to JAXBElement<String> using MapStruct

2.7k views Asked by At

so I was playing a bit with Mapstruct, reading the reference Documentation for the Version 1.1.0.Final, and arrived at the point: implicit type conversions

where is defined the following statement:

Between JAXBElement < T> and T

I tried that, but the error what I received was:

Can't map property "java.lang.String xmlElement" "javax.xml.bind.JAXBElement<java.lang.String> xmlElement". 
Consider to declare/implement a mapping method:
javax.xml.bind.JAXBElement<java.lang.String> map(java.lang.String value)".

I know thisi is the same thread asCan't map property when using MapStruct but since then Mapstruct released a new version.

Am I doing something wrong or this feature really is missing?

Thank you.

3

There are 3 answers

0
Filip On

Mapping from JAXBElement<T> to T works out of the box. For the reverse you need to make sure that the ObjectFactory(ies) are in the Mapper#uses, MapStruct uses those methods to create the types.

You can also have a look at this integration test.

2
Jan Mares On

In case this happens on Java 9 or higher and you use implementation of type JAXBElement from maven library (in my case'javax.xml.bind:jaxb-api') make sure it is on the classpath of the annotation processor - this resolved the issue for me.

0
aee On

If your JAXBElement was generated by a wsdl client generator (eg. xjc) you need to provide the corresponding ObjectFactory.class generated by the client generator:

@Mapper(uses = ObjectFactory.class)
public interface OrderMapper {
    Order orderEntityToExternalOrder(OrderEntity orderEntity);
}

See: MapStruct 1.0.0.Beta1 is out with JAXB support, custom factories, decorators and more