如何使用mapStruct映射使用@XMLSeeAlso注释的JAXB元素? [英] How to map JAXB elements annotated with @XMLSeeAlso using mapStruct?

查看:1089
本文介绍了如何使用mapStruct映射使用@XMLSeeAlso注释的JAXB元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将具有一些JAXB元素的bean映射为@XmlSeeAlso,@ XMLElement,@ XmlSchemaType作为该类的属性。

I'm trying to map a bean which has some JAXB elements like @XmlSeeAlso, @XmlElement, @XmlSchemaType as properties for that class.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Customer")
@XmlSeeAlso({PersonalCustomer.class, BusinessCustomer.class})
public class Customer extends Role {

 @XmlElement(name = "AMLLineOfBusiness")
private LOB amlLineOfBusiness;
 // 50 odd properties
 //some properties with XmlElement/XmlSchemaType 
 // getters and setters   
}

@Mapper
public interface CustomerMapper {
     PersonalCustomer personcalCustomerToPersonalCustomer(PersonalCustomer pc);
@Mappings({
    /*Several other ignore mappings*/
    @Mapping(target="AMLLineOfBusiness",ignore=true)
    })
     Customer customerToCustomer(Customer customer);
   }

现在,我在使用mapStruct进行映射时面临以下问题。

Now I'm facing below issues when mapping using mapStruct.


  1. 未映射Bean PersonalCustomer。我在
    回复中没有看到它。但是在使用推土机时效果很好。我需要做的就是
    在dozer config xml中定义它的映射。我在
    mapStruct中尝试过类似的东西。我在mapper
    接口中定义了personcalCustomerToPersonalCustomer方法,其中包含所需的映射并忽略了不是必需的字段。虽然mapperImpl中有
    实现,但我没有看到它在impl类的任何地方使用

  1. The bean PersonalCustomer is not being mapped. I don't see it in the response. But it works perfectly when using dozer. All I need to do is define it in it's mapping in dozer config xml. I tried similar thing in mapStruct. I defined personcalCustomerToPersonalCustomermethod in mapper interface with required mappings and ignoring not required fields. Although implementation is there in the mapperImpl but I don't see it being used anywhere in the impl class.

虽然忽略具有@XmlElement或@XmlSchemaType的字段,
我在生成impl代码时遇到编译错误。下面是错误
statck trace。当我使用amlLineOfBusiness时出现编译错误。

While ignoring the fields which have either @XmlElement or @XmlSchemaType, I'm getting compilation error while generating impl code. Below is the error statck trace. I got compilation error when I used amlLineOfBusiness.

[70,2]错误:结果类型com.role.Customer中的未知属性amlLineOfBusiness。你的意思是lineOfBusiness吗?
[错误] - > [帮助1]
org.apache.maven.lifecycle.LifecycleExecutionException:无法执行目标org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile
项目服务-jar上的(default-compile):编译失败
C:\ Perforce \ service-jar \2018.08.0 \ service-jar\src\main \ java \ com\mapstruct\mapper \CustomerMapper.java:[70,2]错误:结果类型为com.role.Customer的未知属性amlLineOfBusiness。你的意思是lineOfBusiness吗?
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder $ 1.call(MultiThreadedBuilder.java:188)
at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder $ 1.call(MultiThreadedBuilder.java:184)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors $ RunnableAdapter.call(Executors。 java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1 149)
at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
由... :org.apache.maven.plugin.compiler.CompilationFailureException:编译失败

[70,2] error: Unknown property "amlLineOfBusiness" in result type com.role.Customer. Did you mean "lineOfBusiness"? [ERROR] -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project service-jar: Compilation failure C:\Perforce\service-jar\2018.08.0\service-jar\src\main\java\com\mapstruct\mapper\CustomerMapper.java:[70,2] error: Unknown property "amlLineOfBusiness" in result type com.role.Customer. Did you mean "lineOfBusiness"? at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108) at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call(MultiThreadedBuilder.java:188) at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call(MultiThreadedBuilder.java:184) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure

当我使用
的名称值(@XmlElement(名称)时,解决了上述问题=AMLLineOfBusiness))要忽略的注释。我无法理解这里到底发生了什么。

The above is resolved when I used the name value of (@XmlElement(name="AMLLineOfBusiness")) annotation to ignore. I couldn't understand what exactly going on here.

默认情况下,mapStruct也会映射超类的字段。在我的例子中,
Customer类也获得Role的属性。除了在每个属性上使用ignore = true之外,没有其他选项
。这是一种忙乱,因为我是
有50个奇数字段和数十个类似的映射器,基于服务
的要求。我希望mapStruct具有默认忽略所有字段的功能,或者
一种仅映射指定字段的策略。

By default mapStruct is mapping fields of super class too. In my case, Customer class also getting properties of Role. There is no other option except using ignore=true on each property. This is kind of hectic as I'm having 50 odd fields and tens of similar mappers based on service requirement. I wish mapStruct had feature to ignore all fields by default or a strategy to map only specified fields.

请注意,源和目标类型在此处相同。我只需根据要求映射某些字段。我被困在这里。我非常感谢你的帮助。

Note that both source and destination types are same here. I only need to map certain fields depending on the requirement. I'm stuck here. I highly appreciate your help.

推荐答案

看起来你有3个不同的问题。

It seems like you have 3 different problems.


  1. 您尝试实现的目的是让MapStruct检测 Customer (或参见 @XmlSeeAlso )并使用您需要的方法。在MapStruct中无法自动执行此操作。有关现有功能请求,请参见#131

  2. 当您没有正确定义属性时,会发生这种情况。 MapStruct实际上只查看getter和setter(不在字段中)。所以如果你得到的是 getAM 那么你的 @Mapping(target =AMLLineOfBusiness,ignore = true)

  3. 这与此问题。也许你可以试试重用地图配置

  1. It seems what you are trying to achieve is for MapStruct to detect all possible implementations for Customer (or see @XmlSeeAlso) and use the method you need. This is not possible automatically in MapStruct. See #131 for an existing feature request.
  2. This should happen when you have not defined the property correct. MapStruct actually looks into the getters and setters only (not in the field). So if you getter is getAM then your @Mapping(target = "AMLLineOfBusiness", ignore = true)
  3. This is similar with this question. Maybe you can try Reusing mapping configurations

1的可能解决方案是你的实例。

A possible solution for 1 would be you to an instance of on your side.

@Mapper
public interface CustomerMapper {
    PersonalCustomer personcalCustomerToPersonalCustomer(PersonalCustomer pc);

    default Customer customerToCustomer(Customer customer) {
        if (customer instanceOf PersonalCustomer) {
            return personalCustomerToPersonalCustomer((PersonalCustomer) pc);
        } else if (customer instanceOf BusinessCustomer) {
            return businessCustomerToBusinessCustomer((BusinessCustomer) pc);
        }
    }
}

此类事情的原因是MapStruct是一个注释处理器,因此它在编译期间生成代码。另一方面,Dozer正在处理运行时信息。 Dozer可以在运行时获取类并选择正确的方法。 MapStruct无法推断出所有可能的实现。

The reason for such things is that MapStruct is an annotation processor so it generated code during compilation time. On the other side Dozer is working with runtime information. Dozer can get the class during runtime and pick the right method. MapStruct cannot deduce all the possible implementations.

这篇关于如何使用mapStruct映射使用@XMLSeeAlso注释的JAXB元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆