首页 > 解决方案 > JDK11/EAP7 升级后 XML 解析中断

问题描述

我正在升级一个应用程序EAP6.4/JDK8 -> EAP7.2/JDK11,我的 XML 解析似乎不一样。在升级之前,我能够将以下 XML 解析为带注释的对象。

没有编译或运行时错误,应用程序部署。

如果我从 XML 中删除前面app:的内容,它将转换,否则一切都是null. 有没有办法在忽略命名空间的同时解组这个 xml app:

输出

XML{id='null', time=null, type='null', description='null', time=null}

XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<app:exampleXML xmlns:app="http://www.example.com/schemas/schema">
<app:id>app-id</app:id>
<app:time>2020-06-05T13:17:00.899Z</app:time>
<app:type>test</app:type>
<app:description>Test</app:description>
</app:exampleXML>

目的

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.time.Instant;
import java.util.UUID;

@XmlRootElement(name = "exampleXML", namespace="http://www.example.com/schemas/schema")
@XmlAccessorType(XmlAccessType.FIELD)
public class XML {
    public static final int DESCRIPTION_LENGTH = 4000;

    @XmlElement(name = "id", required = true)
    private String id;


    @XmlElement(name = "time", required = true)
    @XmlJavaTypeAdapter(InstantXmlAdapter.class)
    @JsonSerialize(using = InstantJsonSerializer.class)
    @JsonDeserialize(using = InstantJsonDeserializer.class)
    private Instant time;

    @XmlElement(name = "type", required = true)
    private CheckpointLevel type;

    @XmlElement(name = "description")
    @XmlJavaTypeAdapter(CDATAXmlAdapter.class)
    private String description;

编辑:

我正在jakarta我的 gradle 构建中实现以解决 jdk11 中缺少 jaxb 的问题。

implementation "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2"
implementation "org.glassfish.jaxb:jaxb-runtime:2.3.2"

标签: javaxmljakarta-eejaxbjboss-eap-7

解决方案


从版本 9 开始,JAXB 已从 JDK 中删除。您需要将它们添加为单独的依赖项。如果您使用的是 Maven 或 Gradle,您可以使用 Maven 存储库:

<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-core</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.3.0</version>
</dependency>

推荐阅读