java 使用 MapStruct 时无法映射属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34672216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't map property when using MapStruct
提问by zygimantus
I am using MapStruct library to map objects but I got this error:
我正在使用 MapStruct 库来映射对象,但出现此错误:
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)".
无法将属性“java.util.Date aDate”映射到“javax.xml.bind.JAXBElement ADATE”。考虑声明/实现一个映射方法:“javax.xml.bind.JAXBElement map(java.util.Date value)”。
My question: WHERE should I decleare this mapping method?
我的问题:我应该在哪里取消这种映射方法?
回答by zygimantus
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)
回答by Gunnar
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 guidefor further details
- 使您的映射器成为抽象类而不是接口,并直接在映射器类中实现该方法
- 在另一个类上实现该方法并将此方法声明为您的映射器“已使用”;有关更多详细信息,请参阅参考指南
Btw. the mapping should be done automatically if you are using XMLGregorianCalendar
or JAXBElement<XMLGregorianCalendar>
instead of the JAXBElement
raw type.
顺便提一句。如果您使用XMLGregorianCalendar
或JAXBElement<XMLGregorianCalendar>
而不是JAXBElement
原始类型,则映射应自动完成。