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

2024-04-05

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

我正在关注创始人的博客(@XmlPath 上的博客 http://blog.bdoughan.com/2010/07/xpath-based-mapping.html)使其工作,但由于某种原因,@XmlPath没有影响,并且输出 XML 与博客中所示不匹配。我遵循了博客中提到的所有过程,并在这里提到了各种答案。

我所有的课程都在model.jaxb包裹。另外,我还创建了一个jaxb.properties该包中的文件。

以下是我的Customer class:

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 class:

@Getter
@Setter
@NoArgsConstructor
public class Address {

  private String street;

}

以下是Demo class:

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

}

我得到的 XML 有和没有@XmlPath实际上是一样的。所以看起来像@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在编组期间踢以便我可以在编组期间获得预期的标签?

**** 已编辑 *****

我正在使用 Java 版本 15.0.01Intellij IDE File -> Project Structure。我看到一个答案提到了Moxy直接在上面不起作用Java 11 回答Java 11.0以上 https://stackoverflow.com/a/60285555/7584240.

所以我做了以下事情:

  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提到的步骤进行操作在他的回答中 https://stackoverflow.com/questions/39388961/not-able-to-configure-moxy-using-jaxb/60285555#60285555.

  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.class and Demo.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.activation, javax.activation, javax.validation, org.glassfish.jaxb等等。这些在其他答案中都提到过,所以我正在尝试一切。最好删除所有内容并重新开始,并仅添加需要的内容。对于本例,仅提到了 2 个step-2就足够了。

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

  3. 确保清理并重建 Maven 项目,因为有时dependencies保存时不会直接添加pom.xml文件。至少就我而言,当我使用时Intellij IDE。每次添加依赖项时,我都会进行清理和构建,以便确保一切正常Maven side.

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

@XmlPath 在 JAXB 编组期间没有影响 的相关文章

随机推荐