gpt4 book ai didi

javax.xml.bind.JAXBElement 无法转换为 com.oracle.xmlns.communications.ordermanagement.CreateOrderRequestType

转载 作者:行者123 更新时间:2023-12-02 08:58:28 27 4
gpt4 key购买 nike

这是我正在进行的操作。我生成了 WSDL,并得到了 CreateOrderRequestType.java

但是当我解码时,它正在寻找 CreateOrder。如何获取 CreateOrder 类。


@WebMethod(operationName = "CreateOrder", action = "http://xmlns.oracle.com/communications/ordermanagement/CreateOrder")
@WebResult(name = "CreateOrderResponse", targetNamespace = "http://xmlns.oracle.com/communications/ordermanagement", partName = "CreateOrderResponse")
public CreateOrderResponseType createOrder(
@WebParam(name = "CreateOrder", targetNamespace = "http://xmlns.oracle.com/communications/ordermanagement", partName = "CreateOrderRequest")
CreateOrderRequestType createOrderRequest)
throws CreateOrderFaultMsg, CreateOrderFaultMsg1, CreateOrderFaultMsg3
;

public static CreateOrderRequestType convertXmlStringToJavaObject(String xmlString) {
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(CreateOrderRequestType.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

StringReader reader = new StringReader(xmlString);
CreateOrderRequestType msg = (CreateOrderRequestType) unmarshaller.unmarshal(reader);
return msg;
} catch (JAXBException e) {
throw new OSMException("Unable to Convert XML String to Java Objects " + e.getMessage(), e);
}

}

public CreateOrderResponseType createAncillariesBySoloOrderId(String soloOrderId) {
String message = findOrderBySoloOrderId(soloOrderId);
//CreateOrderRequestType createOrderRequestType = null;
message = "<CreateOrder xmlns=\"http://xmlns.oracle.com/communications/ordermanagement\">"
+ message
+ "</CreateOrder>";
log.info("message before converting to Java Object = " + message);
CreateOrderRequestType createOrderRequestType = com.spectrum.order.util.OrderUtil.convertXmlStringToJavaObject(message);
log.info("Msg Object = " + createOrderRequestType.toString());

return osmClient.createOrder(createOrderRequestType);
}

My xmlString
<CreateOrder xmlns="http://xmlns.oracle.com/communications/ordermanagement">
<msg xmlns=""><head><Client>Charter</Client><Routing>8260|1700|0090</Routing></head></msg>
</CreateOrder>


最佳答案

看起来,在解码期间返回了 JAXBElement 的实例。您可以检查返回的类型并进行适当的处​​理:

        Object obj = unmarshaller.unmarshal(reader);

CreateOrderRequestType msg = null;

if (obj instanceof CreateOrderRequestType) {
msg = (CreateOrderRequestType) obj;
}

if (obj instanceof JAXBElement) {
msg = ((JAXBElement<CreateOrderRequestType>) obj).getValue();
}

if (msg == null) {
throw new OSMException("Unable to Convert XML String to Java Objects ");
}

顺便说一句,最好将变量 JAXBContext jaxbContext; 设为类变量,并且不在服务调用期间启动它,而是在构造函数或某些初始化方法中启动它,因为 JAXBContext 的初始化是时间消耗操作,通常应该发生一次。 JAXBContext 是线程安全的,使其成为类变量不应影响服务功能。

关于javax.xml.bind.JAXBElement 无法转换为 com.oracle.xmlns.communications.ordermanagement.CreateOrderRequestType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60343004/

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