12

I am using MapStruct library to map objects but I got this error:

Can't map property "java.util.Date aDate" to "javax.xml.bind.JAXBElement ADATE". Consider to declare/implement a mapping method: "javax.xml.bind.JAXBElement map(java.util.Date value)".

My question: WHERE should I decleare this mapping method?

zygimantus
  • 3,649
  • 4
  • 39
  • 54
  • What does your current mapping look like? That will inform the answer to the question. AKA, can you please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? – M. Justin Jul 19 '23 at 18:35

2 Answers2

26

I solved this issue by writing another class:

public class DateMapper {

    public JAXBElement<XMLGregorianCalendar> map(Date value) {

        // conversion here

        return atswer;
    }
}

and using this annotation:

@Mapper(uses=DateMapper.class)
zygimantus
  • 3,649
  • 4
  • 39
  • 54
12

There are two alternatives:

  • Make your mapper an abstract class instead of an interface and implement that method directly in the mapper class
  • Implement the method on another class and declare this one as "used" by your mapper; See the reference guide for further details

Btw. the mapping should be done automatically if you are using XMLGregorianCalendar or JAXBElement<XMLGregorianCalendar> instead of the JAXBElement raw type.

Gunnar
  • 18,095
  • 1
  • 53
  • 73