简体   繁体   English

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

[英]How to read the header in the web server response

Tell me how to read the header in the web server's response via the @WebMethod and @WebResult annotations.告诉我如何通过@WebMethod@WebResult注释读取 Web 服务器响应中的标头。 Of course, I can do this by SOAPConnection and parsing SOAPMessage, but there is a lot of functionality on javax.jws and I would like to unify everything.当然,我可以通过 SOAPConnection 和解析 SOAPMessage 来做到这一点,但是 javax.jws 上有很多功能,我想统一一切。 I need value from <osb:Backend/> .我需要<osb:Backend/>

Server response:服务器响应:

<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>
.......

Interface declaration:接口声明:

@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;

Request class:请求类:

@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;
....

Request example:请求示例:

<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>

1. I haven't used jws, and haven't tried this, but it may help: 1. 我没有用过 jws,也没有试过这个,但它可能有帮助:

Try to add header option to your @WebResult annotation.尝试将header选项添加到@WebResult注释中。

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

public abstract boolean header

"If true, the result is pulled from a message header rather then the message body." “如果为 true,则结果是从消息标题而不是消息正文中提取的。”

But seems like in order to do so you have to write appropriate class for JAXB (I'm not sure).但似乎为了做到这一点,您必须为 JAXB 编写适当的类(我不确定)。

2. Instead, I did it straightforward, and used this method: 2.相反,我做得很简单,并使用了这种方法:

Your business data is located inside message body - between <getEmployeePacketInfoRequest2/> tags.您的业​​务数据位于消息正文内 - <getEmployeePacketInfoRequest2/>标签之间。 This is what JAXB creates your GetEmployeePacketInfoResponse class objects from.这就是 JAXB GetEmployeePacketInfoResponse创建GetEmployeePacketInfoResponse类对象的内容。 So, if you want to obtain anything outside this tags, you have to get it from the whole SOAP response.因此,如果您想获取此标记之外的任何内容,则必须从整个 SOAP 响应中获取它。 But you don't need to parse it manually - javax.xml.soap.SOAPMessage has inbuilt getSOAPHeader() method, which does what you need.但是您不需要手动解析它 - javax.xml.soap.SOAPMessage 具有内置的getSOAPHeader()方法,它getSOAPHeader()您的需求。 Then just transform it to DOM, and do getElementsByTagName() or getElementsByTagNameNS() .然后只需将其转换为 DOM,并执行getElementsByTagName()getElementsByTagNameNS()

implemented the second sentence执行第二句

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();
            }
...

You need to use annotated parameter in your class method like this:您需要在类方法中使用带注释的参数,如下所示:

    @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;

pay attention to header = true and mode = WebParam.Mode.IN注意header = truemode = WebParam.Mode.IN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM