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

如何解决@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 方面一切正常。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-