jaxb 相关问题

用于XML绑定的Java体系结构是将XML用作域对象的Java标准。它提供了一种将Java类映射到XML表示的简单机制。

Spring MVC:没有预设内容类型为“null”的 [class com.gcu.model.OrderList] 转换器

我的 Spring MVC 应用程序遇到问题,我尝试返回 OrderList 类型的对象作为 HTTP 响应,但收到以下警告消息: 警告 21264 --- ...

回答 1 投票 0

Java 17 - java.lang.NoClassDefFoundError:javax/xml/bind/annotation/XmlElement

我有一个 Java17 的 gradle 项目,在运行测试时出现以下错误: java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlElement 测试如下: 公开

回答 1 投票 0

如何使用gradle运行jaxb xjc任务?

我为 jaxb/xjc 选择了 3 个 gradle 插件,但没有一个对我有用。 对于每种情况,我都将 maven 模型 xsd maven-4.0.0.xsd https://maven.apache.org/xsd/maven-4.0.0.xsd 放在 src/main/xsd 中。 (...

回答 1 投票 0

JAXB 解组不起作用。预期的元素是(无)

我正在尝试解组 XML。 这就是我的 XML 的样子 我正在尝试解组 XML。 这就是我的 XML 的样子 <DeviceInventory2Response xmlns="http://tempuri.org/"> <DeviceInventory2Result xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <Obj123 xmlns=""> <Id>1</Id> <Name>abc</Name> </Obj123> <Obj456 xmlns=""> . . . 我正在尝试获取 Obj123 下的 Id 和 Name。但是,当我运行 unmarshal 命令时,出现以下错误。 An Error: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://tempuri.org/", local:"DeviceInventory2Response"). Expected elements are (none) 我的代码在主类中看起来像这样: Obj123 myObj123 = (Obj123) unmarshaller.unmarshal(inputSource); 我的 Obj123 类如下所示: package com.myProj.pkg; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name="Obj123") public class Obj123 { private String Id; private String Name; public String getId() { return Id; } public String getName() { return Name; } } 我认为通过设置 XMLRootElement 我应该能够跳过 XML 的前两行,但这似乎没有发生。有什么想法吗? 编辑: 这就是我的 JAXB 上下文的制作方式: JAXBContext jaxbContext = JAXBContext.newInstance(); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Obj123 obj123 = (Obj123) unmarshaller.unmarshal(xmlStreamReader); 我通过添加解决了问题 @XmlRootElement(name="abc_xxx") 到 Root 类。(其中 abc_XXX 是 XML 的根标签) eclipse生成的JAXB类没有在我的根类中添加这个注解。 JAXB 实现将尝试匹配文档的根元素(而不是子元素)。 如果您想解组到 XML 文档的中间,那么您可以使用 StAX 解析文档,将 XMLStreamReader 推进到所需的元素,然后对其进行解组。 了解更多信息 http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html 更新 现在我收到以下错误。一个错误: javax.xml.bind.UnmarshalException - 带有链接异常: [javax.xml.bind.UnmarshalException:意外元素(uri:“”, 本地:“Obj123”)。预期元素为(无)]。 A JAXBContext 只知道你告诉它的类。而不是: JAXBContext jaxbContext = JAXBContext.newInstance(); 你需要做: JAXBContext jaxbContext = JAXBContext.newInstance(Obj123.class); 使用 ObjectFactory 类代替,如 JAXBContext jaxbContext = null; try { jaxbContext = JAXBContext.newInstance(ObjectFactory.class); } catch (JAXBException e) { e.printStackTrace(); } JAXBElement<ObjectFactory> applicationElement = null; try { applicationElement = (JAXBElement<ObjectFactory>) unmarshaller.unmarshal(Thread.currentThread().getClass() .getResourceAsStream(fileName)); } catch (JAXBException e) { e.printStackTrace(); } 试试这个就能解决上述问题。我的问题已经解决了 当我将导入语句从 javax.xml.bind.annotation.XmlRootElement 更新为 @jakarta.xml.bind.annotation.XmlRootElement 时,问题已解决

回答 4 投票 0

Spring boot - 服务器无法识别 HTTP 标头 SOAPAction 的值

我想使用jaxb使用肥皂服务。 jaxb 生成的请求是 ...

回答 3 投票 0

如何使用@JmsListener获取转换后的对象

我正在使用 Spring 和 Jaxb 来监听 JMSQueue,然后将 JMS 消息解组到 java 对象中。然后我希望在我的 @JmsListener 端点上获取该 Java 对象。但相反,我...

回答 1 投票 0

将 HyperJaxb3 包含为 Maven 依赖项时出现 ServiceConfigurationError

我知道 Hyperjaxb3 库对我的项目非常有用,阅读了多个站点的一些描述,并决定将其嵌入到我的 Spring-Hibernate 项目中。 我有...

回答 1 投票 0

HyperJaxb3 项目发生了什么?

我知道 Hyperjaxb3 库对我的项目非常有用,阅读了多个站点的一些描述,并决定将其嵌入到我的 Spring-Hibernate 项目中。 我有...

回答 2 投票 0

发送字符串而不是 JAXBElement<String>

我正在使用 Java 项目中的 .NET Web 服务。我正在使用 Netbeans 8.2 并导入了 Web 服务。当我创建复杂对象时,问题就出现了,Netbeans 或 Java 转换了 e...

回答 1 投票 0

JAXB xjc:简单绑定不会为基类生成@XmlRootElement

我正在使用 JAXB 2.0。我在 XSD 文件中定义了各种元素和类型。这是一个例子: 我正在使用 JAXB 2.0。我在 XSD 文件中定义了各种元素和类型。这是一个例子: <xs:element name="Person" type="Person" /> <xs:complexType name="Person"> <xs:attribute name="name" type="xs:string"/> </xs:complexType> <xs:element name="Musician" type="Musician"/> <xs:complexType name="Musician"> <xs:complexContent> <xs:extension base="Person"> <xs:attribute name="instrument" type="xs:string"/> </xs:extension> </xs:complexContent> </xs:complexType> <xs:element name="People" type="People"/> <xs:complexType name="People"> <xs:sequence> <xs:element name="person" type="Person" minOccurs="0" maxOccurs="Unbounded/> </xs:sequence> </xs:complexType> 正如您从上面的模式示例中看到的,我们有一个有名字的人,还有一个音乐家,他也是一个人(尽管这可能会引起一些争论,但那是另一个论坛的事)。还有一个 People 元素,它本质上是 Person 类型的集合。 我的绑定文件中有以下内容: <?xml version="1.0" encoding="UTF-8"?> <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0" xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc"> <jaxb:globalBindings optionalProperty="wrapper"> <xjc:simple/> </jaxb:globalBindings> 我对这些对象的预期用途是: 一个人可以作为一个单独的编组元素出现,也可以是一个人对象的一部分 如果 Person 是 People 对象的一部分,则 xsi:type 应指示它是普通 Person 还是音乐家。 因此,我需要生成的 Java 类同时包含 @XmlRootElement 注释和 @XmlType 注释。 xjc:simple 绑定为 Musician 创建两个注释,但仅为 Person 创建 @XmlType。因此,当我编组一个 Person 对象时,我得到的只是: <?xml version="1.0" encoding="UTF-8"?> 而我想看到的是: <?xml version="1.0" encoding="UTF-8"?> <Person name="John Doe"/> 对于 People 对象,我想看到: <?xml version="1.0" encoding="UTF-8"?> <People> <person name="John Doe" xsi:type="Person"/> <person name="Keith Richards" xsi:type="Musician"/> </People> 我已经了解了与 xjc 的简单绑定,它适用于继承层次结构中的所有最低级别。但是,基类最终没有 @XmlRootElement 注释。对于我正在处理的用例,基类必须能够编组为顶级元素和其他元素的成员。欢迎任何建议。 可以使用https://github.com/highsource/jaxb2-annotate-plugin <?xml version="1.0" encoding="UTF-8"?> <jaxb:bindings xmlns:annox="http://annox.dev.java.net" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc annox"> <jaxb:bindings node="//xs:complexType[@name='Person']"> <annox:annotate> <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="person"/> </annox:annotate> </jaxb:bindings> </jaxb:bindings>

回答 1 投票 0

Quarkus 3.8.x Java 17 NoClassDefFoundError:javax/xml/bind/JAXBException 依赖冲突

对于 Apache Camel Routes 应用程序,我已从 Quarkus 2.x.x 迁移到 Quarkus 3.8.3,因此从 Java 11 迁移到 Java 17。 当尝试处理 JAXBException 情况时,我遇到了:

回答 1 投票 0

在 Spring 6 和 Java 17 中将 TransformerFactory 属性从 javax.xml.Transform 迁移到 Jakarta

我们正在将应用程序从 Java 11 和 Spring 5 升级到 Spring 6 和 Java 17。作为此迁移的一部分,我们遇到了一个属性集: System.setProperty("javax.xml.

回答 1 投票 0

在 Java 中将字符串验证为 XML

字符串strXML =“abc9876543210” 如何验证是否...

回答 2 投票 0

为什么这个 Enum 类没有 JAXB 编组?

我有一个使用 JAXB 和 MOXy 2.3.2 的 Java 8 应用程序,它可以很好地处理除最近引入的 Enum 之外的所有属性:UserProfileExtensionGenderEnum.java,这是...

回答 1 投票 0

使用部署在 Azure 应用服务中的 JDK 17 Spring Boot 应用程序对 JobRunr 计划任务中的 JAXBException 进行故障排除

仅当在部署在 Azure 上的 JDK 17 和 Spring Boot 上使用 JobRunr 在开发环境中运行计划任务时,我才会遇到 JAXBException。当我调用相同的方法时,不会出现此问题

回答 1 投票 0

在 eclipse 中从 WSDL 生成 XSD

有没有办法在eclipse IDE中从WSDL文件生成XSD。项目要求从WSDL生成XSD,然后使用jaxb自动生成所有java类。我也用eclips...

回答 2 投票 0

JAXBException:属性出现在 @XmlType.propOrder 中,但不存在此类属性

我有一个保存命令可以进行简单的编组: //保存命令 ... 尝试 { JAXBContext 上下文 = JAXBContext.newInstance(CollectionManager.class); 火星...

回答 1 投票 0

将 Soap XML 响应转换为对象

我是使用 SOAP API 的新手 我有来自 API 的肥皂响应 我是 SOAP API 的新手 我有来自 API 的肥皂响应 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <LoginResponse xmlns="http://test.org/ADMail_Service"> <LoginResult> <ErrorMessage>Successful login</ErrorMessage> <Status>true</Status> </LoginResult> </LoginResponse> </soapenv:Body> </soapenv:Envelope> 我正在尝试将其转换为一个对象。 通过在线阅读文章,我尝试使用 JAXB 来执行此操作,但我的对象是空的。 这是读取响应的代码。我将响应写入 xml 文件以进行测试: try { XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml")); xsr.nextTag(); // Advance to Envelope tag xsr.nextTag(); // Advance to Body tag xsr.nextTag(); xsr.nextTag(); JAXBContext jc = JAXBContext.newInstance(LoginResult.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); JAXBElement<LoginResult> je = unmarshaller.unmarshal(xsr, LoginResult.class); System.out.println(je.getName()); System.out.println(je.getValue()); } catch (XMLStreamException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } LoginResult班: public class LoginResult { private String errorMessage; private String status; public String getErrorMessage() { return errorMessage; } public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } } 提前致谢! 您可以使用此代码检索 POJO,还可以将 @XmlRootElement 作为标头添加到 POJO。 (我没有测试下面的代码) XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml")); xsr.nextTag(); // Advance to Envelope tag xsr.nextTag(); // Advance to Body tag xsr.nextTag(); xsr.nextTag(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); StringWriter stringWriter = new StringWriter(); transformer.transform(new StAXSource(xsr), new StreamResult(stringWriter)); StringReader sr = new StringReader(stringWriter.toString()); JAXBContext jaxbContext = JAXBContext.newInstance(LoginResult.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); LoginResult loginResult = (LoginResult) unmarshaller.unmarshal(sr); 编辑: 我为你找到了解决方案: @XmlRootElement(name = "LoginResult", namespace = "http://test.org/ADMail_Service") @XmlAccessorType(XmlAccessType.FIELD) public class LoginResult { @XmlElement(name = "ErrorMessage", namespace = "http://test.org/ADMail_Service") private String errorMessage; @XmlElement(name = "Status", namespace = "http://test.org/ADMail_Service") private String status; public String getErrorMessage() { return errorMessage; } public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } } public static void main(String[] args) { try { XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml")); xsr.nextTag(); // Advance to Envelope tag xsr.nextTag(); // Advance to Body tag xsr.nextTag(); xsr.nextTag(); JAXBContext jc = JAXBContext.newInstance(LoginResult.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); JAXBElement<LoginResult> je = unmarshaller.unmarshal(xsr, LoginResult.class); System.out.println(je.getName()); System.out.println(je.getValue()); System.out.println(je.getValue().getErrorMessage()); } catch (XMLStreamException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } IMO,您应该考虑使用工具来处理 SOAP 消息,而不是自己处理。 示例: Java EE 5 教程:使用 JAX-WS 创建简单的 Web 服务和客户端 Spring Boot:使用 SOAP 消息 编辑 关于您的评论有一些话要说,所以我将我的答案放在这里。 首先, 我与 API 无关,我所做的只是发出 POST 请求... 您与 API 没有任何关系,但您向 API 发出 POST 请求。 我觉得这是一个比喻吧?... 并且没有 wsdl.... 通过这个小技巧,您几乎总能获得 SOAP Web 服务的 WSDL。只需在 SOAP Web 服务 URL 末尾添加 ?wsdl。 示例: 这是网络上 SOAP Web 服务的 URL(真实的):http://www.webservicex.com/stockquote.asmx 您可以像这样获取它的 WSDL :http://www.webservicex.com/stockquote.asmx?wsdl 所以唯一的选择是解析响应 IMO,软件开发中的问题几乎总是有不止一种解决方案。 有时,我发现了这段用于将soapxml对象解析为java对象的代码。 private <T> T getJavaObjectFromSoapXml(String responseFilePath, Class<T> clazz) { try { XMLInputFactory xif = XMLInputFactory.newFactory(); StreamSource xml = new StreamSource(getClass().getResourceAsStream(responseFilePath)); XMLStreamReader xsr = xif.createXMLStreamReader(xml); xsr.nextTag(); while (!xsr.getLocalName().equalsIgnoreCase(clazz.getSimpleName())) { xsr.nextTag(); } JAXBContext jc = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = jc.createUnmarshaller(); JAXBElement<T> je = unmarshaller.unmarshal(xsr, clazz); return je.getValue(); } catch (Exception e) { e.printStackTrace(); return null; } } public <T> T getJavaObjectFromSoapXml(String response, Class<T> clazz) { try { XMLInputFactory xif = XMLInputFactory.newFactory(); StreamSource xml = new StreamSource(response); XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(response)); xsr.nextTag(); while (!xsr.getLocalName().equalsIgnoreCase(clazz.getSimpleName())) { log.info("TAG :{}",xsr.nextTag()); } Transformer transformer = TransformerFactory.newInstance().newTransformer(); StringWriter stringWriter = new StringWriter(); transformer.transform(new StAXSource(xsr), new StreamResult(stringWriter)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(stringWriter.toString()))); JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); T t = (T) unmarshaller.unmarshal(document); return t; } catch (Exception e) { e.printStackTrace(); return null; } } public static <T> T unmarshallSoapResponse(final byte[] soapMessage, Class<T> clazz) throws IOException, SOAPException, JAXBException { SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(soapMessage)); Unmarshaller unmarshaller = JAXBContext.newInstance(clazz).createUnmarshaller(); return (T) unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument()); }

回答 5 投票 0

对参数化类型的类型参数应用约束

我正在尝试对 JAXB 类的 List 属性强制实施 @Pattern 约束。 与此类似: 公开名单 <@Pattern(regex="test") String> 名称; 如何通过绑定方式存档...

回答 1 投票 0

Java,如何在springboot Java中使用Soap XML api?

我是Java世界的新手,我正在使用Springboot框架和Eclipse。 我有相关服务的架构 xsd 和 wsdl 文件。 我从 xsd 和 wsdl 创建了 JAXB 类。 什么会是最好的

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.