Chapter 2 soap Exception Handling and handler handling

Source: Internet
Author: User
Tags webservice annotation

Both the client and server are Java projects. First, list the server code.

 

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

 

package com.jadyer.service;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import com.jadyer.exception.UserException;@WebService(targetNamespace="http://blog.csdn.net/jadyer")public interface HelloService {@WebResult(name="sayHelloResult")public String sayHello(@WebParam(name="name")String name);@WebResult(name="loginResult")public String login(@WebParam(name="username")String username,@WebParam(name="password")String password) throws UserException;}

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

 

 

Package COM. jadyer. service; import javax. JWS. handlerchain; import javax. JWS. webService; import COM. jadyer. exception. userexception; @ WebService (endpointinterface = "com. jadyer. service. helloservice ", targetnamespace =" http://blog.csdn.net/jadyer ") @ handlerchain (file =" myhandlerchain. XML ") public class helloserviceimpl implements helloservice {@ overridepublic string sayhello (string name) {system. out. println ("receive the name = [" + name + "] ...... "); If (null = Name) {return" Hello, world ";} else {return" hello, "+ name ;}@ overridepublic string login (string username, string password) throws userexception {system. out. println ("receive the username = [" + username + "], password = [" + password + "] ...... "); If (" admin ". equals (username) & "Hongyu ". equals (password) {return "User [" + username + "] authenticated ";} throw new userexception ("User [" + username + "] authentication failed ");}}

The following is a custom server exception class userexception. Java

 

 

Package COM. jadyer. exception; // do not use runtimeexception here // because runtimeexception will cause the server to throw an exception to the client, the server itself throws the same exception // when defining exceptions in WebServices development, pay attention to this public class userexception extends exception {Private Static final long serialversionuid = 6252203957834273236l; Public userexception () {super () ;}public userexception (string message) {super (Message );}}

The following is the custom server handler class licensehandler. Java

 

 

Package COM. jadyer. handler; import Java. util. iterator; import Java. util. set; import javax. XML. namespace. QNAME; import javax. XML. soap. soapbody; import javax. XML. soap. soapenvelope; import javax. XML. soap. soapexception; import javax. XML. soap. soapfault; import javax. XML. soap. soapheader; import javax. XML. soap. soapheaderelement; import javax. XML. soap. soapmessage; import javax. XML. WS. handler. messagecontext; import Jav Ax. XML. WS. handler. soap. soaphandler; import javax. XML. WS. handler. soap. soapmessagecontext; import javax. XML. WS. soap. soapfaultexception;/*** handler compiling step * 1) create a class implementing soaphandler <soapmessagecontext> * 2) compile code in the handlemessage () method * 3) Configure handler, customize an XML with arbitrary name * 4) Start the filter chain on the Service * use @ handlerchain (file = "myhandlerchain. XML ") can * @ create may 17,201 3 12:07:54 am * @ author Xuan Yu 

Next, configure myhandlerchain. XML for handler.

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><javaee:handler-chain><javaee:handler><javaee:handler-class>com.jadyer.handler.LicenseHandler</javaee:handler-class></javaee:handler></javaee:handler-chain></javaee:handler-chains>

Finally, serverapp. Java that publishes the WebService Service

 

 

Package COM. jadyer. server; import javax. XML. WS. endpoint; import COM. jadyer. service. helloserviceimpl;/*** soap Exception Handling and handler handling * @ see namespaces * @ see manually specify the namespace. We recommend that you use the @ WebService annotation in both SEI and SIB, as shown below * @ see @ WebService (targetnamespace = "http://blog.csdn.net/jadyer") * @ see dependencies * @ see process soap messages through handler (handler is similar to a filter, which is divided into soaphandler and logicalhandler) * @ see soaphandler ----- soapmessage information can be obtained * @ see logicalhandler -- only soapbody information can be obtained * @ see the messages sent by the client are always processed by logicalhandler first, followed by soaphandler, the message processing order on the server is the opposite * @ see parameter * @ create may 16,201 3 6:14:10 * @ author Xuan Yu 

 

 

Now, the server code has been released. The following is the client code.

 

First, the handler class headerhandler. Java customized by the client

 

Package COM. jadyer. handler; import Java. io. ioexception; import Java. util. set; import javax. XML. namespace. QNAME; import javax. XML. soap. soapenvelope; import javax. XML. soap. soapexception; import javax. XML. soap. soapheader; import javax. XML. soap. soapmessage; import javax. XML. WS. handler. messagecontext; import javax. XML. WS. handler. soap. soaphandler; import javax. XML. WS. handler. soap. soapmessagecontext; public class headerhandler implements soaphandler <soapmessagecontext >{@ overridepublic set <QNAME> getheaders () {return NULL ;}@ overridepublic void close (messagecontext context) {}@ overridepublic Boolean handlefault (soapmessagecontext context) {system. out. println ("\ nclient. handlefault () is invoked ..... "); Return false ;}@ overridepublic Boolean handlemessage (soapmessagecontext context) {system. out. println ("\ nclient. handlemessage () is invoked ..... "); Boolean isoutbound = (Boolean) context. get (messagecontext. message_outbound_property); If (isoutbound) {soapmessage message = context. getmessage (); try {soapenvelope envelope = message. getsoappart (). getenvelope (); soapheader header = envelope. getheader (); string partname = envelope. getbody (). getchildnodes (). item (0 ). getlocalname (); // system. out. println ("------------------------------------"); // nodelist Nan = envelope. getbody (). getchildnodes (); // system. out. println ("Nan. getlength () = "+ Nan. getlength (); // For (INT I = 0; I <Nan. getlength (); I ++) {// system. out. println (Nan. item (I ). getlocalname (); //} // system. out. println ("----------------------------------"); // Add the header information if ("login ". equals (partname) {If (null = header) {header = envelope. addheader ();} QNAME = new QNAME ("http://blog.csdn.net/jadyer", "licenseinfo", "ns"); header. addheaderelement (QNAME ). setvalue ("jadyer"); message. writeto (system. out) ;}} catch (soapexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} return true ;}}

The following is myhandlerchain. xml used to configure the client handler.

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><javaee:handler-chain><javaee:handler><javaee:handler-class>com.jadyer.handler.HeaderHandler</javaee:handler-class></javaee:handler></javaee:handler-chain></javaee:handler-chains>

Finally, the client calls the server code.

 

Note: The client code is generated by wsimport, see http://blog.csdn.net/jadyer/article/details/8692108

 

Package COM. jadyer. client; import javax. XML. WS. soap. soapfaultexception; import net. csdn. blog. jadyer. helloservice; import net. csdn. blog. jadyer. helloserviceimplservice; import net. csdn. blog. jadyer. userexception_exception; public class clientapp {public static void main (string [] ARGs) {helloservice Server = new helloserviceimplservice (). gethelloserviceimplport (); try {system. out. println (server. sayhello (""); system. out. println (server. login ("admin", "Hongyu");} catch (userexception_exception e) {// catch the exception system that the login () method on the server may throw. out. println ("userexception:" + E. getmessage ();} catch (soapfaultexception e) {// catch the exception system that the server may throw when the soapheader is null. out. println ("soapfaultexception:" + E. getmessage ());}}}

Finally, the console output is also posted during the example running.

 

Below is the console output of the server.

 

Server. handlemessage () is invoked ...... receive the name = [Xuan Yu] ...... server. handlemessage () is invoked ...... server. handlemessage () is invoked ...... the Protocol is valid ...... jadyerreceive the username = [admin], password = [Hongyu] ...... server. handlemessage () is invoked ......

The console output of the client is as follows:

 

 

Client. handlemessage () is invoked ..... client. handlemessage () is invoked ..... hello, Xuan Yu client. handlemessage () is invoked ..... <s: envelope xmlns: S = "http://schemas.xmlsoap.org/soap/envelope/"> <s: Header> <ns: licenseinfo xmlns: NS = "http://blog.csdn.net/jadyer"> jadyer </ns: licenseinfo> </s: header> <s: Body> <NS2: Login xmlns: int32 = "http://blog.csdn.net/jadyer"> <username> admin </username> <password> Hongyu </password> </NS2: login> </S: Body> </S: envelope> client. handlemessage () is invoked ..... user [admin] authentication passed

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.