gpt4 book ai didi

java - 扩展类的 JMapper XML 配置

转载 作者:行者123 更新时间:2023-11-30 10:58:58 25 4
gpt4 key购买 nike

我想配置 xml 文件以映射到扩展类,但得到一个 UndefinedMappingException。我能找到的唯一相关主题是 this

这是我的实现:

JMapper<RequestD, RequestS> mapper =
new JMapper<>(RequestD.class, RequestS.class, "jmapper.xml");
RequestD requestD = mapper.getDestination(someRequestS);

目标类:

public class RequestD extends RequestBase {
private static final long serialVersionUID = 1L;
}

扩展类:

public abstract class RequestBase implements Serializable {
private static final long serialVersionUID = 1L;

private Language language;
private DateTime requestDateTime;
private String currency;
private Order order;

/**
* Getters and Setters
*/

源类:

@XmlRootElement(name = "RequestS")
public class RequestS {

@Valid
protected Language language;
@XmlElement(type = String.class)
@XmlJavaTypeAdapter(Adapter1 .class)
@XmlSchemaType(name = "dateTime")
protected DateTime requestDateTime;
@XmlElement(required = true)
@NotNull
@Valid
protected Order order;

/**
* Getters and Setters
*/

jmapper.xml:

<jmapper>
<class name="package.RequestD">
<attribute name="language">
<value name="language"/>
</attribute>
</class>
</jmapper>

最佳答案

UndefinedMappingException 被抛出是因为属性 language 在目标类 RequestD 中不存在,你不应该尝试映射它。

而是将目标类配置为使用全局映射工具:

<jmapper>
<class name="package.RequestD">
<global/>
</class>
</jmapper>

这将允许找到用于映射的扩展类。

注意:您应该为每个要映射属性的目标类提供配置。下面的示例包括上面未显示的属性,但构成了一个更完整的示例:

<jmapper>
<class name="package.RequestD">
<global/>
</class>

<class name="package.Language">
<attribute name="name">
<value name="name"/>
</attribute>
</class>

<class name="package.Order">
<attribute name="number">
<value name="number"/>
</attribute>
<attribute name="reference">
<value name="reference"/>
</attribute>
</class>
</jmapper>

关于java - 扩展类的 JMapper XML 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32069487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com