XML解析在升级至JDK11/EAP7后出现问题。

huangapple go评论97阅读模式
英文:

XML Parsing Broken after JDK11/EAP7 upgrade

问题

以下是翻译好的部分:

输出

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;
}

编辑:

我正在在我的 Gradle 构建中实现 jakarta,以弥补 JDK 11 中缺少 jaxb 的问题。

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

请注意,以上内容已经完成翻译,如果还有其他需要翻译的内容,请提供。

英文:

I'm upgrading an app from EAP6.4/JDK8 -&gt; EAP7.2/JDK11 and my XML parsing doesn't seem to be working the same. Before the upgrade I was able to parse the following XML into an annotated object.

No compile or runtime errors, app deploys.

If I remove the preceding app: from the XML it'll convert, else everything is null. Is there a way to unmarshall this xml while ignoring the namespace app:?

Output

XML{id=&#39;null&#39;, time=null, type=&#39;null&#39;, description=&#39;null&#39;, time=null}

XML

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

Object

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 = &quot;exampleXML&quot;, namespace=&quot;http://www.example.com/schemas/schema&quot;)
@XmlAccessorType(XmlAccessType.FIELD)
public class XML {
    public static final int DESCRIPTION_LENGTH = 4000;

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


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

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

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

EDIT:

I'm implementing jakarta in my gradle build to account for the lack of jaxb in jdk11.

implementation &quot;jakarta.xml.bind:jakarta.xml.bind-api:2.3.2&quot;
implementation &quot;org.glassfish.jaxb:jaxb-runtime:2.3.2&quot;

答案1

得分: 1

从版本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>
英文:

Starting from version 9 JAXB was removed from JDK. You need to add them as separate dependencies. If you are using Maven or Gradle you can use Maven repository for that:

&lt;dependency&gt;
  &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
  &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
  &lt;version&gt;2.3.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
  &lt;artifactId&gt;jaxb-core&lt;/artifactId&gt;
  &lt;version&gt;2.3.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
  &lt;artifactId&gt;jaxb-impl&lt;/artifactId&gt;
  &lt;version&gt;2.3.0&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年6月5日 21:34:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/62216575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定