In-depth exploration SOAP1.1 - use saaj1.2.1

xiaoxiao2021-03-06  39

The booking author of this article should have aware of the basic specifications of SOAP 1.1, and familiar with J2EE's basic development. If you are not familiar, you can look at my blog: Use SOAP to develop Java Web services - AXIS development plan, detailed criteria You can view the official website of W3C, connect the following: http://www.w3.org/tr/2000/note-soap-20000508/. This article is mainly to explore SAAJ (SOAP WITH ATACHMENT API for Java), JAXM (Java API for XML Messaging) to learn about SOAP's role in J2EE development. JAXM and SAAJ support for B2B and Web service applications, XML-based messaging, support many industry standards, including SOAP and EBXML. Saaj is one of the components of JWSDP. JWSDP has now updated 1.5 version, Saaj can download from Sun's website: http://java.sun.com/xml/downloads/saaj.html, Jaxm is used for XML messages The criterion is not in the release package of JWSDP1.5, you can download: http://java.sun.com/xml/jaxm/downloads/index.html, Because Saaj is based on JavaMailTM API (1.2), JavaBeansTM Activation Framework (JAF) (1.1.3) and JAXP (1.2.6), so I have to download them, and Sun's official website has provided, ok, now we get the following package (pressing the name I downloaded) : Activation.jar (jaf), jaxm-api.jar (jaxm), mail.jar (javamail), saaj-api.jar (saaj), saaj-impl.jar (Saaj), my JAXP is already in JDK1. 5 in 5. There is also a container that supports servlets. Ok, ready to work, let's start our learning: First, saaj can be used to send and receive XML documents as SOAP messages without having to process the basic program structure of the JAXM provider, and no need to handle SOAP-based HTTP request / response. SAAJ was originally an integral part of the JAXM1.0API package, and starting from JAXM1.1, the package is renamed is Saaj1.1API. Let's take a simple SOAP1.1 message: post / stockquote http / 1.1host: www.stockquoteserver.comcontent-type: text / xml; charset = "UTF-8" Content-length: nnnnsoaPaction: "Some-URI"

<----------- This is the envelope sign <----------- this is the message header flag 5 <---------- - This is the main sign Def Saaj API provides us with a high-level SOAP message package interface, such as an envelope interface: javax.xml.soap.soapenvelope, and provides a message header (), which returns a message header: javax.xml.soap.soapheader. So, by calling Saaj interface functions, we can operate the SOAP message. Second, now let's discuss the JAXM that does not use the message exchange provider, the application client sends and receives message operations directly with its remote partners through SOAP (defined the interaction and synchronous communication model of point-to-point, where the sender is sent. The recipient switches the message in the form of a request and response. The sender sends a message and waits for the response of the lock target location.

Transmitting step: 1) Creating a SOAP connection; 2) Creating a message factory; 3) Creating a message; 4) Fill messages; 5) Add a message; 6) Add SOAP attachment; 7) Send a message and receive a response; 8) Close the provider Connection; one is one of my writes: / ** * sender.java * Copyright 2005-2-10 * / Import javax.xml.soap.soapConnectionFactory; import javax.xml.soap.soapexception; import javax.xml.soap .SOAPConnection; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPHeader; import javax .xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPHeaderElement; import javax.xml.soap.Name; import javax.xml.soap.SOAPElement; import java.net.URL; import javax.xml.messaging.URLEndpoint; import javax.activation.DataHandler; import java.io.IOException; public class Sender {public SOAPMessage getMessage () throws SOAPException, Exception {// message factory MessageFactory msgFactory = MessageFactory.newInstance (); SOAPMESSAGE MESSAGE = msgfactory.createMessage (); // Get a SOAPPART object SOA PPart soapPart = message.getSOAPPart (); // get the envelope SOAPEnvelope soapEnvelope = soapPart.getEnvelope (); // get the message header SOAPHeader soapHeader = soapEnvelope.getHeader (); // get the SOAP body SOAPBody soapBody = soapEnvelope.getBody (); // add a header element SOAPHeaderElement headerElement = soapHeader.addHeaderElement (soapEnvelope.createName ( "StudentNo", "stu", "http://www.cun.edu.cn/jws")); headerElement.addTextNode ( "JWS0229043") ; // Add Message Main Name BodyName = SOAPENVELOPE.CREATENAME ("GetStudentInfo", "STU", "http://www.cun.edu.cn/jws");

SOAPBodyElement bodyElement = soapBody.addBodyElement (bodyName); Name eleName = soapEnvelope.createName ( "StudentName"); SOAPElement se = bodyElement.addChildElement (eleName); se.addTextNode ( "Wang wenyin"); // add SOAP attachment URL url = new uRL ( "http://img20.photo.163.com/gdanthrowwy/5123911/80707051.jpg"); dataHandler dataHandler = new dataHandler (url); // use the JAF message.addAttachmentPart (message.createAttachmentPart (dataHandler) ); // update SOAP message message.saveChanges (); return message;} public void send (SOAPMessage message) throws SOAPException, IOException {// create a SOAP connector SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance (); SOAPConnection sc = scf.createConnection ( ); // Send a SOAP message to the destination, and return a message urlendpoint urlendpoint = new urlendpoint ("http: // localhost / saaj / studentinfoservlet"); soapMessage response = sc.call (Message, UrlendPoint); if (Response! = NULL) {// Output SOAP message to the console System.Out.Println ("Receive SOAP MESSAGE from localhost:"); response.writeto (system.out);} else {system.e rr.println ( "No response received from partner!");} sc.close ();} public static void main (String [] args) throws SOAPException, Exception {Sender sender = new Sender (); SOAPMessage message = sender. GetMessage (); sender.send (message);}} then compile, pay attention to the set of ClassPath variables to add the above packages (you can set a script to complete, if you are familiar with Ant, then it is more simple). After the compilation is successful, we waited for a sender.class file, which is a sender file. When you run Java Sender, you will send the SOAP message to our companion http: // localhost / saaj / studentinfoservlet, and wait back. Here we continue to write a servlet to receive the message just sent. Third, the receiver servlet application saaj.war.

/ ** * JAXMReceiveServlet.java * Copyright 2005-2-10 * / import javax.xml.messaging.JAXMServlet; import javax.xml.messaging.ReqRespListener; import javax.xml.soap.MessageFactory; import javax.servlet.ServletException; Import javax.xml.soap.soapMl.Soap.soapenvelop; import javax.servlet.servletconfig; import java.io.fileoutputstream; import java.io.file;

public class JAXMReceiveServlet extends JAXMServlet implements ReqRespListener {static MessageFactory mf = null; // Create a message factory static {try {mf = MessageFactory.newInstance ();} catch (Exception e) {e.printStackTrace ();}}; public void init (ServletConfig sc) throws ServletException {super.init (sc);} // pass over the SOAP message processing, and returns a SOAP message public SOAPMessage onMessage (SOAPMessage msg) {SOAPMessage resp = null; try {System.out.println ("Incoming Message:"); Msg.writtream (New FileoutPutStream (New File ("../ WebApps / SOAPMESSAGE.XML"))); // Create a return message resp = mf.createMessage (); SOAPENVELOPE SE = resp.getsoAppart (). getENvelope (); se.getBody (). AddChildElement (SE.CREATENAME ("responsemessage")). AddTextNode ("Received Message, Thanks"); return resp;} catch (Exception E) {E .printStackTrace ();} return resp;}} then add the associated classpath into, compile (no words, to check the programming of servlet, limited file limited) Web.xml Deployment file: studentinfoservlet jaxmreceiveservlet 1 studentinfoservlet / studentinfoservlet

To this, our basic work is complete, deploy program saaj, when you visit http: // localhost / saaj / studentinfoservlet, will return an error message because it is useful here to use the SOAP protocol. Fourth, the test If the deployment is successful, then we start our test. We continue to step the second step, Java Sender, then wait, we created a SOAP message in the sender (with an attachment for a JPG picture), and sent it to the servlet container (my Tomcat), in the server side Try to the message and create a soapMessage.xml file under the WebApps folder, write the received SOAP information and return a SOAP message. After waiting for a while, the Sender party returns an XML file in the SOAP format and outputs on the console. Ok, today's in-depth exploration of SOAP is here. If you have any questions about this article, please leave a message to the author. Author contact address: sidney.j.yellow@gmail.com

转载请注明原文地址:https://www.9cbs.com/read-59014.html

New Post(0)