gpt4 book ai didi

java - 将类编码到 XSD :Anytype xml

转载 作者:行者123 更新时间:2023-11-30 06:25:04 26 4
gpt4 key购买 nike

我的问题是我有一个 MessageList 类型的类,它包含 Message 项,定义如下:

<xsd:complexType name="MessageType">
<xsd:sequence>
<xsd:any/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="Message" type="tns:MessageType"/>
<xsd:complexType name="MessageListType">
<xsd:sequence>
<xsd:element name="Message" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="MessageList" type="tns:MessageListType"/>

我的消息类看起来像(这里省略了 getter 和 setter)

public class Message
{
private String referenceId;
private String messageType;
private Timestamp createdTime;
private String transactionIdForeignKey;
private String xmlDetail;
}

当我尝试在我的 servlet 中编码此类时:

         List<Message> retrievedMessages = new ArrayList<Message>();
retrievedMessages.add(new Message);
retrievedMessages.add(new Message);

MessageListType messageListType = new MessageListType();
for(Message message: retrievedMessages){
messageListType.getMessage().add(message);
}

我收到以下错误:

[#|2013-04-25T16:43:40.434+0100|WARNING|sun-appserver2.1|javax.enterprise.system.stream.err|_ThreadID=188;_ThreadName=p: thread-pool-1; w: 76;_RequestID=40c6fe10-c12f-4d0b-8912-c960381910d7;|
javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: class uk.co.test.test.audit.model.Message nor any of its super class is known to this context.]
at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:322)
at com.sun.enterprise.jbi.serviceengine.comm.WrappedMessage.writePayloadTo(WrappedMessage.java:419)
at com.sun.enterprise.jbi.serviceengine.comm.WrappedMessage$DocumentStyleWrapper.wrap(WrappedMessage.java:215)
at com.sun.enterprise.jbi.serviceengine.comm.WrappedMessage.wrap(WrappedMessage.java:170)
at com.sun.enterprise.jbi.serviceengine.comm.ProviderInOut.send(ProviderInOut.java:143)
at com.sun.enterprise.jbi.serviceengine.bridge.transport.NMRServerConnection.sendResponse(NMRServerConnection.java:81)
at com.sun.enterprise.jbi.serviceengine.bridge.transport.JBIAdapter$WSToolkit.handle(JBIAdapter.java:127)
at com.sun.enterprise.jbi.serviceengine.bridge.transport.JBIAdapter.handle(JBIAdapter.java:86)
at com.sun.enterprise.jbi.serviceengine.bridge.JAXWSMessageProcessor.doWork(JAXWSMessageProcessor.java:93)
at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:555)
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: class uk.co.test.test.audit.model.Message nor any of its super class is known to this context.]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:282)
at com.sun.xml.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:100)
at com.sun.xml.bind.api.Bridge.marshal(Bridge.java:141)
at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:315)

有人知道为什么会这样吗?我猜正在执行编码的 servlet 需要知道要查看哪个类,但我不确定该怎么做?

最佳答案

如果您想在类里面如此精确,则需要在 Schema 中更加精确。

JAXB 依赖于定义结构的模式,该结构与您希望 JAXB 解码到的结构相同。

为了说明,让我们使用 xjc 编译您的模式- 这将创建 JAXB 可以自动编码并对其进行注释的类。

这是类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MessageType", propOrder = {
"any"
})
public class MessageType {

@XmlAnyElement(lax = true)
protected Object any;

public Object getAny() {
return any;
}

public void setAny(Object value) {
this.any = value;
}

}

您可以看到,JAXB 只能从您的模式中判断出您有一个 MessageType。包含未知数量的东西的类 - <xsd:any/> - 例如,JAXB 无法确定您有一个 String称为 referenceId需要设置。

如果您可以更改架构,则更新为

<xsd:complexType name="MessageType">
<xsd:attribute name="referenceId" type="xsd:string"/>
<xsd:attribute name="messageType" type="xsd:string"/>
<xsd:attribute name="transactionIdForeignKey" type="xsd:string"/>
<xsd:attribute name="xmlDetail" type="xsd:string"/>
<xsd:attribute name="createdTime" type="xsd:integer"/>
</xsd:complexType>

告诉 JAXB 构建类所需的信息(在这种情况下,TimeStamplong - 要解码为 TimeStamp,您需要一些 custom logic)。

生成的类现在类似于您自己的类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MessageType")
public class MessageType {

@XmlAttribute(name = "referenceId")
protected String referenceId;
@XmlAttribute(name = "messageType")
protected String messageType;
@XmlAttribute(name = "transactionIdForeignKey")
protected String transactionIdForeignKey;
@XmlAttribute(name = "xmlDetail")
protected String xmlDetail;
@XmlAttribute(name = "createdTime")
protected BigInteger createdTime;

public String getReferenceId() {
return referenceId;
}

public void setReferenceId(String value) {
this.referenceId = value;
}

public String getMessageType() {
return messageType;
}

public void setMessageType(String value) {
this.messageType = value;
}

public String getTransactionIdForeignKey() {
return transactionIdForeignKey;
}

public void setTransactionIdForeignKey(String value) {
this.transactionIdForeignKey = value;
}

public String getXmlDetail() {
return xmlDetail;
}

public void setXmlDetail(String value) {
this.xmlDetail = value;
}

public BigInteger getCreatedTime() {
return createdTime;
}

public void setCreatedTime(BigInteger value) {
this.createdTime = value;
}

}

可以看到xml中的属性直接映射到类中的字段。您还可以看到 JAXB 使用的注释,这些是 not required但有助于告诉它什么映射到哪里。

如果您无法更改架构,那么您将需要实现自定义逻辑来搜索您的 <xsd:any/>对于相关属性和 map ,然后手动。 This如果你沿着那条路走下去,应该会让你开始。

关于java - 将类编码到 XSD :Anytype xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16219579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com