2

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.

David Laci
  • 146
  • 1
  • 12

3 Answers3

0

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.

Filip
  • 19,269
  • 7
  • 51
  • 60
0

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.

Jan Mares
  • 795
  • 10
  • 22
  • How to specify the classpath of javax.xml.bind etc for annotation processor in Gradle ? – Ken Chen Oct 14 '19 at 23:14
  • Add dependency into compileOnly or compile configuration like so - `compileOnly('...')` or `compile('...')` where ... is your library. – Jan Mares Oct 17 '19 at 23:01
0

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

aee
  • 543
  • 3
  • 14