Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't map property when using MapStruct

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?

like image 609
zygimantus Avatar asked Jan 08 '16 08:01

zygimantus


People also ask

How do you ignore property in MapStruct?

We can ignore unmapped properties in several mappers by setting the unmappedTargetPolicy via @MapperConfig to share a setting across several mappers. We should note that this is a simple example showing the minimal usage of @MapperConfig, which might not seem much better than setting the policy on each mapper.

What is MappingTarget in MapStruct?

Annotation Type MappingTargetDeclares a parameter of a mapping method to be the target of the mapping. Not more than one parameter can be declared as MappingTarget . NOTE: The parameter passed as a mapping target must not be null .

Does MapStruct work with Lombok?

edited. mapstruct in combination with Lombok, is unable to correctly resolve mappings specified as property fields only, when correctly annotated with Lombok annotations @DaTa or more verbosely using @Setter and @getter only. Except getters and setter are also provided to the source and target classes.

Does MapStruct use reflection?

During compilation, MapStruct will generate an implementation of this interface. This implementation uses plain Java method invocations for mapping between source and target objects, i.e. no reflection or similar.


1 Answers

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)
like image 140
zygimantus Avatar answered Nov 08 '22 07:11

zygimantus