Chapter 03rd creating soap messages using SAAJ

Source: Internet
Author: User
Tags xpath soapui

This is a Java project. Put the server code first.

The first is sei, that is, helloservice. Java, the server interface class.

Package COM. jadyer. service; import Java. util. list; import javax. JWS. webparam; import javax. JWS. webresult; import javax. JWS. webService; import COM. jadyer. model. user; @ webservicepublic interface helloservice {@ webresult (name = "loginresult") Public String login (@ webparam (name = "username") string username, @ webparam (name = "password") string password ); // here, both the return value and parameter name are defined as user // to generate both the SOAP request message and Response Message <user> <ID>... </ID> </user> regular XML // you can use jaxb to convert messages to JavaBean @ webresult (name = "user ") public user adduser (@ webparam (name = "user") User user); @ webresult (name = "user") public list <user> List ();}

Then there is SIB, that is, the server interface implementation class helloserviceimpl. Java

Package COM. jadyer. service; import Java. util. arraylist; import Java. util. list; import javax. JWS. webService; import COM. jadyer. model. user; @ WebService (endpointinterface = "com. jadyer. service. helloservice ") public class helloserviceimpl implements helloservice {// simulate a small warehouse in the memory Private Static list <user> Users = new arraylist <user> (); // initialize a user public helloserviceimpl () {users. add (new user (1, "admin", "Administrator", "222222") ;}@ overridepublic string login (string username, string password) {system. out. println ("login () is invoked ...... "); For (User: Users) {If (user. getUserName (). equals (username) & user. getPassword (). equals (password) {return "User [" + username + "] logon successful" ;}} return "User [" + username + "] Logon Failed ";} @ overridepublic user adduser (User user) {system. out. println ("adduser () is invoked ...... "); users. add (User); Return user ;}@ overridepublic list <user> List () {system. out. println ("list () is invoked ...... "); Return users ;}}

The following is the XML ing entity class user. java used by the server.

Package COM. jadyer. model; import javax. XML. BIND. annotation. xmlrootelement; @ xmlrootelementpublic class user {private int ID; private string username; private string nickname; private string password;/* setter and getter of the four attributes */public user () {} public user (int id, string username, string nickname, string password) {This. id = ID; this. username = username; this. nickname = nickname; this. password = password ;}}

The mainapp. java used to publish the service

package com.jadyer.server;import javax.xml.ws.Endpoint;import com.jadyer.service.HelloServiceImpl;public class MainApp {public static void main(String[] args) {Endpoint.publish("http://127.0.0.1:8888/myHelloService", new HelloServiceImpl());}}

So far, the WebServices server has been released.

The following is a demo of compiling a client with SAAJ.

Package COM. jadyer. client; import Java. io. stringreader; import Java. io. stringwriter; import java.net. URL; import javax. XML. BIND. jaxbcontext; import javax. XML. BIND. marshaller; import javax. XML. BIND. unmarshaller; import javax. XML. namespace. QNAME; import javax. XML. soap. messagefactory; import javax. XML. soap. soapbody; import javax. XML. soap. soapbodyelement; import javax. XML. soap. soapenvelope; import javax. XML. soap. s Oapmessage; import javax. XML. soap. soappart; import javax. XML. transform. source; import javax. XML. transform. transformerfactory; import javax. XML. transform. dom. domresult; import javax. XML. transform. stream. streamsource; import javax. XML. WS. dispatch; import javax. XML. WS. service; import javax. XML. XPath. XPath; import javax. XML. XPath. xpathconstants; import javax. XML. XPath. xpathfactory; import Org. w3C. dom. document; Im Port Org. w3C. dom. node; import Org. w3C. dom. nodelist; import COM. jadyer. model. user;/*** use SAAJ to create a SOAP message and access the service * @ see SAAJ (soap with attachments API for Java) is jaxm (Java API for XML messaging) the basic of a branch * @ see consumer * @ see Web Service is to send and receive messages in standard format. This standard format is soap * @ see. You can use SAAJ to manually generate soap messages, concurrently sent to the server, you can also receive the server response * @ see ------------- Author * @ see here we recommend a free tool required for WebServices soapui (http://www.soapui.org/) * @ see after it is used to create a new soapui project, we can clearly see the SOAP message format of the SOAP message to be sent and the response * @ see, which is of great help to understand this article .. similar to Apache tcpmon (http://ws.apache.org/commons/tcpmon/) * @ see Tables * @ creat E Mar 18,201 3 2:47:18 * @ author Xuan Yu 

The console outputs of the WebServices server and the client are listed respectively.

23:00:13 COM. sun. XML. internal. WS. model. runtimemodeler getrequestwrapperclass information: dynamically creating request Wrapper class COM. jadyer. service. jaxws. list2013-3-28 23:00:13 COM. sun. XML. internal. WS. model. runtimemodeler getresponsewrapperclass information: dynamically creating response wrapper Bean class COM. jadyer. service. jaxws. listResponse2013-3-28 23:00:13 COM. sun. XML. internal. WS. model. runtimemodeler getrequestwrapperclass information: dynamically creating request Wrapper class COM. jadyer. service. jaxws. login2013-3-28 23:00:13 COM. sun. XML. internal. WS. model. runtimemodeler getresponsewrapperclass information: dynamically creating response wrapper Bean class COM. jadyer. service. jaxws. loginResponse2013-3-28 23:00:13 COM. sun. XML. internal. WS. model. runtimemodeler getrequestwrapperclass information: dynamically creating request Wrapper class COM. jadyer. service. jaxws. addUser2013-3-28 23:00:13 COM. sun. XML. internal. WS. model. runtimemodeler getresponsewrapperclass information: dynamically creating response wrapper Bean class COM. jadyer. service. jaxws. adduserresponselogin () is invoked ...... adduser () is invoked ...... list () is invoked ......
<SOAP-ENV: envelope xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV: Header/> <SOAP-ENV: Body> <NN: Login xmlns: nn = "http://service.jadyer.com/"> <username> admin </username> <password> 222222 </password> </NN: Login> </SOAP-ENV: Body> </SOAP-ENV: envelope> initiate a request: invoking ...... <s: envelope xmlns: S = "http://schemas.xmlsoap.org/soap/envelope/"> <s: Header/> <s: Body> <NS2: loginresponse xmlns: <loginresult> User [admin] login successful </loginresult> </NS2: loginresponse> </S: Body> </s: envelope> received a response: User [admin] logon succeeded ------------------------------------------------------------------------- assembled: <NN: adduser xmlns: nn = "http://service.jadyer.com/"> <user> <ID> 3 </ID> <nickname> Hong Yu </nickname> <password> 888888 </password> <username> Hongyu </ username> </user> </NN: adduser> received response: Hongyu received response: 888888 bytes <SOAP-ENV: envelope xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV: Header/> <SOAP-ENV: body> <NN: List xmlns: NN = "http://service.jadyer.com/"/> </SOAP-ENV: Body> </SOAP-ENV: envelope> <s: envelope xmlns: S = "http://schemas.xmlsoap.org/soap/envelope/"> <s: Header/> <s: Body> <NS2: listresponse xmlns: NS2. = "http://service.jadyer.com/"> <user> <ID> 1 </ID> <nickname> administrator </nickname> <password> 222222 </password> <username> admin </ username> </user> <ID> 3 </ID> <nickname> Hong Yu </nickname> <password> 888888 </password> <username> Hongyu </Username> </user> </NS2: listresponse> </S: Body> </S: envelope> received response: Admin received response: Administrator received response: 222222 received response: Hongyu received response: Hong Yu received response: 888888

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.