@XmlPath 在 JAXB 编组期间没有影响

问题描述

我正在尝试使用 JaxB Marshalling 方法创建 XML。我想跳过某些孩子的父标签,或者可能会为某个元素添加一个新的 XML标签。因此,我尝试使用 @XmlPath 中的 import org.eclipse.persistence.oxm.annotations.XmlPath;

我正在关注创始人 (Blog on @XmlPath) 的博客以使其正常运行,但由于某种原因,@XmlPath 没有影响,并且输出 XML 与博客中所示不匹配。我遵循了博客中提到的所有流程,并在此处提到了各种答案。

我所有的类都在 model.jaxb 包中。此外,我在该包中创建了一个 jaxb.properties 文件

以下是我的 Customer 课:

import javax.xml.bind.annotation.XmlAccesstype;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@Getter
@Setter
@NoArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccesstype.FIELD)
public class Customer {

  @XmlPath("contact-info/billing-address")
  private Address billingAddress;

  @XmlPath("contact-info/shipping-address")
  private Address shippingAddress;

}

以下是我的 Address 课:

@Getter
@Setter
@NoArgsConstructor
public class Address {

  private String street;

}

以下是 Demo 类:

public class Demo {

  public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Customer.class);

    Customer customer = new Customer();

    Address billingAddress = new Address();
    billingAddress.setStreet("1 Billing Street");
    customer.setBillingAddress(billingAddress);

    Address shippingAddress = new Address();
    shippingAddress.setStreet("2 Shipping Road");
    customer.setShippingAddress(shippingAddress);

    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
    m.marshal(customer,System.out);
  }

}

我使用和不使用 @XmlPath 得到的 XML 实际上是一样的。所以看起来 @XmlPath 在编组期间没有影响。

<customer>
    <billingAddress>
        <street>1 Billing Street</street>
    </billingAddress>
    <shippingAddress>
        <street>2 Shipping Road</street>
    </shippingAddress>
</customer>

我在我的 jaxb.properties 中创建了 package model.jaxb 文件内容如下:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

根据博客输出的 XML 应该是这样的:

<customer>
   <contact-info>
      <billing-address>
         <street>1 Billing Street</street>
      </billing-address>
      <shipping-address>
         <street>2 Shipping Road</street>
      </shipping-address>
   </contact-info>
<customer>

我不确定我做错了什么,因为我无法按预期获得 XML。我尝试了很多东西,但都没有奏效,所以在这里发布相同的内容

有人可以帮助我如何在编组期间踢出 @XmlPath 以便我在编组期间获得预期的标签吗?

**** 编辑 *****

根据 Intellij IDE File -> Project Structure,我使用的是 Java 版本 15.0.01。我看到一个答案,其中提到 Moxy 不能直接在 Java 11 Answer Java 11.0 above 上方工作。

所以我做了以下事情:

  1. 添加了上面答案中提到的依赖项和 build,所以我的 pom.xml 看起来像这样:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
      <modelVersion>4.0.0</modelVersion>
          <groupId>model.jaxb</groupId>
        <version>1.0-SNAPSHOT</version>
    
      <artifactId>model-jaxb</artifactId>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>8</source>
              <target>8</target>
            </configuration>
          </plugin>
        </plugins>
        <resources>
          <resource>
            <directory>src/main/java</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
        </resources>
      </build>
    
      <dependencies>
    
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.16</version>
          <scope>provided</scope>
        </dependency>
    
        <dependency>
          <groupId>com.sun.activation</groupId>
          <artifactId>javax.activation</artifactId>
          <version>1.2.0</version>
        </dependency>
    
        <dependency>
          <groupId>javax.xml.bind</groupId>
          <artifactId>jaxb-api</artifactId>
          <version>2.3.1</version>
        </dependency>
    
        <dependency>
          <groupId>com.sun.xml.bind</groupId>
          <artifactId>jaxb-core</artifactId>
          <version>2.3.0.1</version>
        </dependency>
    
        <dependency>
          <groupId>com.sun.xml.bind</groupId>
          <artifactId>jaxb-impl</artifactId>
          <version>2.3.1</version>
        </dependency>
    
        <dependency>
          <groupId>org.eclipse.persistence</groupId>
          <artifactId>eclipselink</artifactId>
          <version>2.7.6</version>
        </dependency>

    <dependency>
      <groupId>jakarta.xml.bind</groupId>
      <artifactId>jakarta.xml.bind-api</artifactId>
      <version>2.3.2</version>
    </dependency>

    <dependency>
      <groupId>org.glassfish.jaxb</groupId>
      <artifactId>jaxb-runtime</artifactId>
      <version>2.3.2</version>
    </dependency>
        
      </dependencies>
    
    </project>

当我运行程序时,出现错误Exception in thread "main" java.lang.NoClassDefFoundError: jakarta/xml/bind/JAXBException

Caused by: java.lang.classNotFoundException: jakarta.xml.bind.JAXBException

我尝试了 StackOverflow for this error and most of the answers mentioned adding the dependency` 中提到的很多内容,我添加了答案中提到的所有依赖项。我不确定出了什么问题。

有人可以帮助我解决这个问题吗?

解决方法

最终在@andrewjames 的帮助下找到了解决方案。发布相同的解决方案,以便有人可以快速找到解决方案,而不会像我一样花费一整天:)

您也可以按照@andrewjames in his answer 提到的步骤进行操作。

  1. 确保在与域类相同的包中只有一个 jaxb.properties 文件的副本(在本例中将在编组期间使用的那个是 Customer.class)。文件内容应为:

    jakarta.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

2.从您的 pom.xml 文件中删除所有依赖项,然后添加以下 2 个依赖项:

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>3.0.1</version>
</dependency>

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>3.0.0</version>
</dependency>
  1. 确保您的所有类(在本例中为 Customer.classDemo.ckass)都使用来自 import jakarta.xml.bind.JAXBContext; 的导入。

之前取自 import javax.xml.bind.JAXBContext; 的应该替换为 import jakarta.xml.bind.JAXBContext;

我正在对 Demo.class 进行更改,但忘记在 Customer.class 中进行更改,因此发现了一些 defclass 问题。因此,请确保您的所有类都使用来自 Jakarta 而不是来自 Javax 的导入。

  1. 将以下 <build> 添加到您的 pom.xml
      <build>
        <resources>
          <resource>
            <directory>src/main/java</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
        </resources>
      </build>
  1. 进行这些更改后,运行 Demo.class 这应该会为您提供预期的 XML。 @XmlPath 也应该按预期工作。

以下是我犯的一些错误。您可以检查您是否没有制作它们:

  1. 我有太多的依赖项,有些也重复了。例如来自 com.sun.activationjavax.activationjavax.validationorg.glassfish.jaxb 等的依赖项。其他答案中提到了这些,所以我正在尝试一切。最好删除所有内容并重新开始并仅添加一次需要的内容。对于这种情况,只需 step-2 中提到的 2 个就足够了。

  2. Stackoverflow 上的大部分帖子中提到的答案是针对旧版本的。包括各种网站上的文章和博客。由于在 Java 11 之后发生了一些变化,最好使用最新版本以及本答案或上面链接提供的其他答案中提到的步骤。

  3. 请确保清理并重建 maven 项目,因为有时保存 dependencies 文件时不会直接添加 pom.xml。至少在我使用 Intellij IDE 的情况下。每次添加依赖项时,我都会进行清理和构建,以确保从 Maven 方面一切正常。

这就是我觉得足以完成这项工作的所有要点。如果您仍然面临一些问题,请在其他帖子或此处发表评论。如果可以的话,我会尽力回答。祝你有美好的一天。