我已经使用 Spring Boot 开发了肥皂请求,其中我的肥皂请求在“soapenv:Header”的“”部分中包含自定义牧民。我在检索“”部分中传递的详细信息时遇到问题。没有异常,但我收到的都是后端 RequestHeader 对象的空属性值
我的肥皂请求如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://com/schema/OrderRequest" xmlns:ns="http://com/schema/RequestHeader">
<soapenv:Header>
<ns:RequestHeader>
<ns:name>Name1</ns:name>
</ns:RequestHeader>>
</soapenv:Header>
<soapenv:Body>
.... no issue to retrieve body information
</soapenv:Body>
</soapenv:Envelope>
我尝试使用下面的代码检索标题部分
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "OrderRequest")
@ResponsePayload
public JAXBElement<OrderResponse> createOrder(@RequestPayload JAXBElement<OrderRequest>
createWorkOrderRequestCplxType, @SoapHeader("{http://com/schema/RequestHeader/2/0}RequestHeader")SoapHeaderElement requestHeader) {
log.info("Start createWorkOrder :");
try{
Source bodySource = requestHeader.getSource();
DOMSource bodyDomSource = (DOMSource) bodySource;
JAXBContext context = JAXBContext.newInstance(RequestHeader.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
RequestHeader requestH = (RequestHeader) unmarshaller.unmarshal(bodyDomSource);
log.info("activity name from request header : {}",requestH.getName); //This value is getting empty
}catch (JAXBException e){
e.printStackTrace();
}
}
你能帮我理解我做错了什么吗?
我尝试了多种方法,但没有找到直接在 PayloadRoot 方法中获取 RequesHeader 的 Java 对象的直接解决方案。因此能够使用以下替代方法进行管理:
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "OrderRequest")
@ResponsePayload
public JAXBElement<OrderResponse> createOrder(@RequestPayload JAXBElement<OrderRequest>
createWorkOrderRequestCplxType, MessageContext messgeContext) {
RequestHeader requestHeader = prepareRequestHeader(messageContext);
}
private RequestHeader prepareRequestHeader(MessageContext messageContext) {
RequestHeader requestHeader = new RequestHeader();
SoapHeader soapHeader = ((SoapMessage) messageContext.getRequest()).getSoapHeader();
Iterator<?> elementIter = soapHeader.examineAllHeaderElements();
while (elementIter.hasNext()) {
Object element = elementIter.next();
if (!(element instanceof SoapHeaderElement)) {
continue;
}
Result result = ((SoapHeaderElement) element).getResult();
if (!(result instanceof DOMResult)) {
continue;
}
NodeList nodeList = ((DOMResult) result).getNode().getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.hasChildNodes()) {
String nodeName = node.getNodeName();
String nodeValue = node.getFirstChild().getNodeValue();
addValueToRequestHeader(requestHeader, nodeName, nodeValue);
}
}
}
return requestHeader;
}
private void addValueToRequestHeader(RequestHeader requestHeader, String nodeName, String nodeValue) {
log.info("Start addValueToRequestHeader for nodeName : {} and nodeValue : {}", nodeName, nodeValue);
switch (nodeName) {
case "ns:name":
requestHeader.setActivityName(nodeValue);
break;
case "ns:lastname":
if (nodeValue.equalsIgnoreCase(RequestMessageType.NOTIFICATION.value())) {
requestHeader.setMsgType(RequestMessageType.NOTIFICATION);
} else {
requestHeader.setMsgType(RequestMessageType.REQUEST);
}
break;
case "ns:middlename":
requestHeader.setSenderURI(nodeValue);
break;
default:
break;
}
}