链接异常在从命名空间读取XML时进行编排

huangapple go评论84阅读模式
英文:

Marshalling linking exception when reading XML from namespace

问题

我正在尝试编组对象并将其发送到JMS。
我可能犯了什么错误?

QName qName = new QName("http://schema.gspt.net/EventCanonical/1.0", "OrderType");
JAXBElement<OrderType> jaxbElement = new JAXBElement(qName, OrderType.class, orderType);
jaxb2Marshaller.createMarshaller().marshal(jaxbElement, sw);

javax.xml.bind.MarshalException
- 与链接的异常
[com.sun.istack.internal.SAXException2com.radial.notification.event.OrderType在此上下文中未知]

这是XML中的命名空间

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://schema.gspt.net/EventCanonical/1.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://schema.gspt.net/EventCanonical/1.0" elementFormDefault="qualified"
    attributeFormDefault="unqualified">

以前的传统方法实际上是有效的

JAXBContext jaxbContext = JAXBContext.newInstance(OrderType.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(orderType, sw);
英文:

I am trying to marshall the object and send it to JMS.
What could be the mistake I am doing?

QName qName = new QName(&quot;http://schema.gspt.net/EventCanonical/1.0&quot;,&quot;OrderType&quot;);
JAXBElement&lt;OrderType&gt; jaxbElement = new JAXBElement( qName, OrderType.class,orderType);
jaxb2Marshaller.createMarshaller().marshal(jaxbElement,sw);

javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: com.radial.notification.event.OrderType is not known to this 
context

This is the namespace in XML

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xs:schema xmlns=&quot;http://schema.gspt.net/EventCanonical/1.0&quot; 
xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
targetNamespace=&quot;http://schema.gspt.net/EventCanonical/1.0&quot; elementFormDefault=&quot;qualified&quot; 
attributeFormDefault=&quot;unqualified&quot;&gt;

The classic way of doing it actually working

JAXBContext jaxbContext = JAXBContext.newInstance(OrderType.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(orderType, sw);

答案1

得分: 1

设置绑定的类解决了问题

Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(OrderType.class)
英文:

Setting the classes to bound solved the problem

Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(OrderType.class)

huangapple
  • 本文由 发表于 2020年4月9日 05:32:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61110375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定