如何在Web服务器响应中读取标头

时间:2018-10-16 08:44:35

标签: java xml soap

告诉我如何通过@WebMethod@WebResult批注读取Web服务器响应中的标头。当然,我可以通过SOAPConnection并解析SOAPMessage来做到这一点,但是javax.jws上有很多功能,我想统一所有东西。我需要<osb:Backend/>的价值。

服务器响应:

<soapenv:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <osb:Backend xmlns:osb="http://osb.emias.mos.ru/system">СКУУ</osb:Backend>
        <ipaddr xmlns="https:/bis.skyy.soapHeader/">10.0.5.147</ipaddr>
        <build xmlns="https:/bis.skyy.soapHeader/">1ec22a8</build>
    </env:Header>
    <env:Body xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <getEmployeePacketInfoResponse2 xmlns="http://emias.gov.ru/medempregisterservicetypes/1" xmlns:ns1="http://emias.gov.ru/types/1" xmlns:ns0="http://emias.gov.ru/servicetypes/1">
            <EmployeeList>
.......

接口声明:

@WebResult(name = "Backend", targetNamespace = "http://emias.gov.ru/medempregisterservicetypes/1", partName = "getMedicalEmployeePacketInfo")
@WebMethod
public String getMedicalEmployeePacketInfo2(
    @WebParam(partName = "getMedicalEmployeePacketInfoRequest", name = "getEmployeePacketInfoRequest", targetNamespace = "http://emias.gov.ru/medempregisterservicetypes/1")
    GetEmployeePacketInfoRequest getMedicalEmployeePacketInfoRequest
) throws FaultMessage;

请求类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "requesterSystemCode",
    "healthOrgID",
    "employeeList"
})
@XmlRootElement(name = "getEmployeePacketInfoRequest")
public class GetEmployeePacketInfoRequest {

    @XmlElement(name = "RequesterSystemCode", required = true)
    protected String requesterSystemCode;
    @XmlElement(name = "HealthOrgID")
    protected String healthOrgID;
    @XmlElement(name = "EmployeeList", required = true)
    protected GetEmployeePacketInfoRequest.EmployeeList employeeList;
....

请求示例:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://emias.gov.ru/medempregisterservicetypes/1">
    <soapenv:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:UsernameToken>
                <wsse:Username>SPU/erz</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">emias_erz</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <ns:getEmployeePacketInfoRequest2>
            <ns:RequesterSystemCode>SPU</ns:RequesterSystemCode>
            <ns:HealthOrgID>10000430</ns:HealthOrgID>
            <ns:EmployeeList>
                <ns:EmployeeID>21426012</ns:EmployeeID>
            </ns:EmployeeList>
        </ns:getEmployeePacketInfoRequest2>
    </soapenv:Body>
</soapenv:Envelope>

3 个答案:

答案 0 :(得分:0)

1。我没有使用过jws,也没有尝试过,但这可能会有所帮助:

尝试将header选项添加到@WebResult批注中。

请参见https://docs.oracle.com/javaee/6/api/javax/jws/WebResult.html

public abstract boolean header
  

“如果为true,则从消息头而不是消息正文中提取结果。”

但是为了这样做,您似乎必须为JAXB编写适当的类(我不确定)。

2。相反,我做到了简单明了,并使用了以下方法:

您的业务数据位于消息正文内-<getEmployeePacketInfoRequest2/>标签之间。这就是JAXB从中创建GetEmployeePacketInfoResponse类对象的方式。因此,如果要获取此标记之外的任何内容,则必须从整个SOAP响应中获取。但是您不需要手动解析它-javax.xml.soap.SOAPMessage具有内置的getSOAPHeader()方法,该方法可以满足您的需求。然后只需将其转换为DOM,然后执行getElementsByTagName()getElementsByTagNameNS()

答案 1 :(得分:0)

实施第二句话

import lombok.val;
.... 
val soapPart = resp.getSOAPPart();
            val soapEnvelope = soapPart.getEnvelope();
            val soapHeader = soapEnvelope.getHeader();
            val backendNode = soapHeader.getElementsByTagName("osb:Backend");
            if (backendNode.getLength() > 0) {
                backend = backendNode.item(0).getTextContent();
            }
...

答案 2 :(得分:0)

您需要在类方法中使用带注释的参数,如下所示:

    @WebResult(name = "Backend", targetNamespace = "http://emias.gov.ru/medempregisterservicetypes/1", partName = "getMedicalEmployeePacketInfo")
    @WebMethod
    public String getMedicalEmployeePacketInfo2(
        @WebParam(partName = "header", name = "ipaddr", targetNamespace = "https:/bis.skyy.soapHeader/", header = true, mode = WebParam.Mode.IN)
        String header,
...
        @WebParam(partName = "getMedicalEmployeePacketInfoRequest", name = "getEmployeePacketInfoRequest", targetNamespace = "http://emias.gov.ru/medempregisterservicetypes/1")
        GetEmployeePacketInfoRequest getMedicalEmployeePacketInfoRequest
    ) throws FaultMessage;

注意header = truemode = WebParam.Mode.IN