@XmlPath在JAXB编组期间没有影响 [英] @XmlPath has no impact during the JAXB Marshalling

查看:83
本文介绍了@XmlPath在JAXB编组期间没有影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在跟踪创始人的博客( @XmlPath上的博客)使其正常工作,但由于某些原因, @XmlPath 没有影响,并且输出XML与博客中所示的不匹配.我遵循了博客中提到的所有过程,并且还提到了此处的各种答案.

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

以下是我的 Customer 类:

  import javax.xml.bind.annotation.XmlAccessType;导入javax.xml.bind.annotation.XmlAccessorType;导入javax.xml.bind.annotation.XmlRootElement;导入lombok.Getter;导入lombok.NoArgsConstructor;进口龙目岛.导入org.eclipse.persistence.oxm.annotations.XmlPath;@盖特@Setter@NoArgsConstructor@XmlRootElement@XmlAccessorType(XmlAccessType.FIELD)公共类客户{@XmlPath("contact-info/billing-address")私人地址billingAddress;@XmlPath("contact-info/shipping-address")私人地址shippingAddress;} 

以下是我的 Address 类:

  @Getter@Setter@NoArgsConstructor公共课程地址{私人弦街;} 

以下是 Demo 类:

 公共类Demo {公共静态void main(String [] args)引发异常{JAXBContext jc = JAXBContext.newInstance(Customer.class);客户客户=新客户();地址billingAddress = new Address();billingAddress.setStreet("1 Billing Street");customer.setBillingAddress(billingAddress);地址shippingAddress = new Address();shippingAddress.setStreet("2 Shipping Road");customer.setShippingAddress(shippingAddress);马歇尔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货运路</street></shippingAddress></customer> 

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

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

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

 < customer>< contact-info><计费地址>< street> 1 Billing Street</street></billing-address><送货地址>< street> 2货运路</street></发货地址></contact-info><客户> 

我不确定我在做什么错,因为我无法按预期获取XML.我尝试了很多事情,但没有任何效果,因此在此处发布同样的内容.

有人可以帮助我如何在编组过程中踢 @XmlPath 以便在编组过程中获得期望的标签吗?

****编辑*****

我正在根据 Intellij IDE File->使用Java版本15.0.01.项目结构.我看到一个答案,其中提到 Moxy Java 11 上方不能直接工作答案Java 11.0以上版本.

所以我做了以下事情:

  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>模型-jaxb</artifactId>< build>< plugins>< plugin>< groupId> org.apache.maven.plugins</groupId>< artifactId> maven-compiler-plugin</artifactId><配置>< source> 8</source>< target> 8</target></configuration></plugin></plugins><资源><资源>< directory> src/main/java</directory>< excludes>< exclude> **/*.java</exclude></excludes></resource></resources></build><依赖关系><依赖关系>< groupId> org.projectlombok</groupId>< artifactId> lombok</artifactId>< version> 1.18.16</version>< scope>提供的</scope></dependency><依赖关系>< groupId> com.sun.activation</groupId>< artifactId> javax.activation</artifactId>< version> 1.2.0</version></dependency><依赖关系>< groupId> javax.xml.bind</groupId>< artifactId> jaxb-api</artifactId>< version> 2.3.1</version></dependency><依赖关系>< groupId> com.sun.xml.bind</groupId>< artifactId> jaxb-core</artifactId>< version> 2.3.0.1</version></dependency><依赖关系>< groupId> com.sun.xml.bind</groupId>< artifactId> jaxb-impl</artifactId>< version> 2.3.1</version></dependency><依赖关系>< groupId> org.eclipse.persistence</groupId>< artifactId> eclipselink</artifactId>< version> 2.7.6</version></dependency><依赖关系>< groupId> jakarta.xml.bind</groupId>< artifactId> jakarta.xml.bind-api</artifactId>< version> 2.3.2</version></dependency><依赖关系>< groupId> org.glassfish.jaxb</groupId>< artifactId> jaxb运行时</artifactId>< version> 2.3.2</version></dependency></dependencies></project> 

运行程序时出现错误:线程"main"中的异常;java.lang.NoClassDefFoundError:jakarta/xml/bind/JAXBException

原因:java.lang.ClassNotFoundException:jakarta.xml.bind.JAXBException

我尝试了 StackOverflow上提到的很多错误,并且大多数答案都提到了添加 dependency`,我已经添加了答案中提到的所有依赖关系.我不知道怎么了.

有人可以协助我解决该问题吗?

解决方案

最后,在@andrewjames的大力协助下,终于找到了解决方案.以相同的方式发布解决方案,以便有人可以快速找到解决方案,而不会像我这样终日消磨一整天:)

您还可以按照@andrewjames 中提到的步骤进行操作他的答案.

  1. 确保与域类在同一包中只有一个 jaxb.properties 文件的副本(在编组过程中将使用该副本,在这种情况下,> Customer.class ).该文件的内容应为:

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

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

 < dependency>< groupId> jakarta.xml.bind</groupId>< artifactId> jakarta.xml.bind-api</artifactId>< version> 3.0.1</version></dependency><依赖关系>< groupId> org.eclipse.persistence</groupId>< artifactId> eclipselink</artifactId>< version> 3.0.0</version></dependency> 

  1. 确保所有类(在本例中为 Customer.class 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><资源><资源>< 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 的依赖项>等.在其他答案中提到了这些,因此我正在尝试所有方法.最好删除所有内容并重新开始,并仅添加所需的内容.对于这种情况,在 step-2 中仅提及2个即可.

  2. Stackoverflow 上大多数文章中提到的答案都是针对旧版本的.包括各种网站上的文章和博客.由于 Java 11 之后进行了一些更改,因此最好使用最新版本以及此答案或上面链接提供的其他答案中提到的步骤.

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

这是我认为足以完成这项工作的全部要点.如果您仍然遇到问题,请在其他帖子或此处发表评论.如果可以的话,我会尽力回答.祝你有美好的一天.

I am trying to create XML using the JaxB Marshalling approach. I want to skip the parent tag for certain children or may add a new XML parent tag for a certain element. Hence I am trying to use the @XmlPath from import org.eclipse.persistence.oxm.annotations.XmlPath;.

I am following the blog from the founder (Blog on @XmlPath) to make it work but for some reason, the @XmlPath has no impact and the output XML does not match as shown in the blog. I followed all the process mentioned within the blog and also mentioned on various answers here.

All my classes are within the model.jaxb package. Also, I have created a jaxb.properties file within that package.

Following is my 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;

}

Following is my Address class:

@Getter
@Setter
@NoArgsConstructor
public class Address {

  private String street;

}

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

}

The XML I getting with and without @XmlPath are the same actually. So seems like the @XmlPath has no impact during the marshalling.

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

I have created jaxb.properties file within my package model.jaxb with the content:

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

As per the blog the output XML should be something like this:

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

I am not sure what am I doing wrong due to which I am unable to get the XML as I expect. I tried a lot of things but none worked so posting the same here.

Can someone please help me with how can I make the @XmlPath kicking during the marshalling so that I can get the expected tag during the marshalling?

**** EDITED *****

I am using Java version 15.0.01 as per the Intellij IDE File -> Project Structure. I saw one answer where it mentioned Moxy does not work directly above Java 11 Answer Java 11.0 above.

So I did following things:

  1. I added the dependency and build as mentioned in the above answer so my pom.xml looks something like this:

    <?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>

When I run the program then I get the error: Exception in thread "main" java.lang.NoClassDefFoundError: jakarta/xml/bind/JAXBException

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

I tried a lot of things mentioned on StackOverflow for this error and most of the answers mentioned adding the dependency` I have added all the dependency mentioned in the answer. I am not sure what's going wrong.

Can someone please assist me with how can I fix this issue?

解决方案

Finally was able to find the solution with a lot of assist from @andrewjames. Posting the solution for the same so somebody can find the solution quickly and do not end up spending whole day like me :)

You can also follow the steps mentioned by @andrewjames in his answer.

  1. Make sure you have only ONE copy of jaxb.properties file within the same package as of the domain class (the one which will be used during the marshalling in this case the Customer.class). The content of the file should be:

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

2.Remove all the dependency from your pom.xml file and just add the following 2 dependencies:

<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. Make sure all your classes (in this case Customer.class and Demo.ckass) are using the imports from import jakarta.xml.bind.JAXBContext;.

Previously it was taking from import javax.xml.bind.JAXBContext; should be replaced from import jakarta.xml.bind.JAXBContext;.

I was making changes to Demo.class and forgot to change in Customer.class so was seeing some defclass issue. So make sure all your class uses the imports from Jakarta and not from Javax.

  1. Add the following <build> to your pom.xml:

      <build>
        <resources>
          <resource>
            <directory>src/main/java</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
        </resources>
      </build>

  1. After making these changes run the Demo.class this should give you the expected XML. Also @XmlPath should work as expected.

Following are some of the mistakes I was making. You can check if you are not making them:

  1. I was having too many dependencies and some were duplicated also. Such as dependencies from com.sun.activation, javax.activation, javax.validation, org.glassfish.jaxb, etc. These were mentioned in other answers so I was trying everything. Better remove all and make a fresh start and add only the once needed. For this case, only 2 mentioned in step-2 would be sufficient.

  2. The answers mentioned in most of the post on Stackoverflow is for the old version. Including the articles and blogs on various website. As there has been some changes after Java 11 better use the latest version and the steps mentioned in this answer or the other answer provided with the link above.

  3. Make sure you clean and rebuild the maven project because sometimes the dependencies are not directly added when you save the pom.xml file. At least in my case when I was using the Intellij IDE. I was doing clean and build every time I added the dependency so that I am making sure everything is fine from Maven side.

That's all the points I feel sufficient for making this work. If you are still facing some issue then leave a comment on the other post or here. I will try to answer if I can. Have a nice day.

这篇关于@XmlPath在JAXB编组期间没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆