Saaj-based client

Source: Internet
Author: User

Overview Saaj-soap with Attachments API for Java structure diagram is as follows: Body 1. There are four key parameters for how to obtain the key parameter of a SOAP request:
    1. Xmlns-xml namespaces
      If you do not have a concept for namespaces, refer to: W3school-xml namespaces.
    2. Address of the service
    3. Service's name
    4. Port's name
Get:
    1. The targetnamespace of the root element of the WSDL document can be obtained to xmlns:
    2. Additional three parameters can be obtained in the service element

      Yellow highlighted three places, respectively, can get the service Name,port name, the address of the service.
      It is worth saying that the name of the service here is the same as the name of the root element of the WSDL document, but it is only a coincidence; here is "really", there is just "look like".
2. Parameter declaration
/**address of the service*/Private Static FinalString ADDRESS = "Http://localhost:6666/service/interpret";/**Target Namespace*/Private Static FinalString target_name_space = "http://service.chapter1.jws.sand.ljl.cn/";/**the name of the service*/Private Static FinalString service_name = "Interpretserviceimplservice";/**Name of Port*/Private Static FinalString port_name = "Interpretserviceimplport";

3. Create a message
/*** Create and populate a SOAP message. *@return * @throwsSoapException*/PrivateSoapMessage CreateMessage ()throwsSoapException {//1. Create a messageSoapMessage message =messagefactory.newinstance (). CreateMessage (); //2. Get EnvelopeSoapEnvelope envelope =Message.getsoappart (). Getenvelope (); //3. Get the bodySoapbody BODY =Envelope.getbody (); //4. Constructing interpret elementsQName QName =NewQName (Target_name_space, "interpret", "NS"); //5. Adding elements to the bodySoapbodyelement bodyelement =body.addbodyelement (QName); //6. Adding child elementsBodyelement.addchildelement ("num"). SetValue ("112358")); returnmessage;}

The result of the above code creation:

<xmlns:soap-env= "http://schemas.xmlsoap.org/soap/envelope/">  <soap-env:header/><soap-env:body> 
About QName
New QName ("http://service.chapter1.jws.sand.ljl.cn/", "interpret", "ns");

The three parameters of the preceding code, respectively, represent the namespace, local name, and prefix. The equivalent of creating the following XML element:

<xmlns:ns= "http://service.chapter1.jws.sand.ljl.cn/"/>

About printing SoapMessage

The message can be printed to the specified output stream using Javax.xml.soap.SOAPMessage.writeTo (OutputStream out).
4. Send a message
/*** Sends a SOAP message and returns a SOAP message for the response. *@paramRequest *@return * @throwsmalformedurlexception*/PrivateSoapMessage Send (SoapMessage request)throwsmalformedurlexception {//1. Create a service based on address, ServiceNameURL url =NewURL (ADDRESS); QName ServiceName=NewQName (Target_name_space, service_name); Service Service=service.create (URL, serviceName); //2. Using service, create dispatch based on PortNameQName PortName =NewQName (Target_name_space, port_name); Dispatch<SOAPMessage> dispatch =Service.createdispatch (PortName, SoapMessage.class, Service.Mode.MESSAGE); //3. Sending the requestSoapMessage response =Dispatch.invoke (Request); returnresponse;}

The result of the response obtained by the above code:

<S:envelopexmlns:s= "http://schemas.xmlsoap.org/soap/envelope/">  <S:header/>  <S:body>     <Ns2:interpretresponseXmlns:ns2= "http://service.chapter1.jws.sand.ljl.cn/">       <Chnum>112358</Chnum>     </Ns2:interpretresponse>  </S:body></S:envelope>
5. Parsing results
Document doc = response.getsoappart (). Getenvelope ()       = Doc.getelementsbytagname ("Chnum"). Item (0)       . Gettextcontent ();
The full demo
 Packagecn.ljl.sand.jws.chapter2.client;Importjava.io.IOException;Importjava.net.MalformedURLException;ImportJava.net.URL;ImportJavax.xml.namespace.QName;Importjavax.xml.soap.MessageFactory;ImportJavax.xml.soap.SOAPBody;Importjavax.xml.soap.SOAPBodyElement;ImportJavax.xml.soap.SOAPEnvelope;Importjavax.xml.soap.SOAPException;ImportJavax.xml.soap.SOAPMessage;ImportJavax.xml.ws.Dispatch;ImportJavax.xml.ws.Service;ImportOrg.junit.Assert;Importorg.junit.Test;Importorg.w3c.dom.Document;ImportCn.ljl.sand.jws.chapter1.service.InterpretService;/*** Saaj-based client .<br> * Access to service reference {@linkInterpretservice}. *  * @authorLijinlong **/ Public classsaajclient {/**address of the service*/    Private Static FinalString ADDRESS = "Http://localhost:6666/service/interpret"; /**Target Namespace*/    Private Static FinalString target_name_space = "http://service.chapter1.jws.sand.ljl.cn/"; /**the name of the service*/    Private Static FinalString service_name = "Interpretserviceimplservice"; /**Name of Port*/    Private Static FinalString port_name = "Interpretserviceimplport"; /*** Create and populate a SOAP message. * @return     * @throwsSoapException*/    PrivateSoapMessage CreateMessage ()throwsSoapException {SoapMessage message=messagefactory.newinstance (). CreateMessage (); SoapEnvelope Envelope=Message.getsoappart (). Getenvelope (); Soapbody Body=Envelope.getbody (); QName QName=NewQName (Target_name_space, "interpret", "NS"); Soapbodyelement BodyElement=body.addbodyelement (QName); Bodyelement.addchildelement ("Num"). SetValue ("112358")); returnmessage; }        /*** Sends a SOAP message and returns a SOAP message for the response. * @paramRequest *@return     * @throwsmalformedurlexception*/    PrivateSoapMessage Send (SoapMessage request)throwsmalformedurlexception {URL url=NewURL (ADDRESS); QName ServiceName=NewQName (Target_name_space, service_name); Service Service=service.create (URL, serviceName); QName PortName=NewQName (Target_name_space, port_name); Dispatch<SOAPMessage> dispatch =Service.createdispatch (PortName, SoapMessage.class, Service.Mode.MESSAGE); SoapMessage Response=Dispatch.invoke (Request); returnresponse; } @Test Public voidTest ()throwsSoapException, IOException {soapmessage request=createmessage ();                Request.writeto (System.out); SoapMessage Response=Send (Request);                Response.writeto (System.out); Document Doc=Response.getsoappart (). Getenvelope (). GetBody (). Extractcontentasdocument (); String result= Doc.getelementsbytagname ("Chnum"). Item (0). Gettextcontent (); Assert.assertequals ("112358", result); }}



From for notes (Wiz)



Saaj-based client

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.