Use JAXWS to establish WebService clients and implement handler validation examples for SOAP messages _java

Source: Internet
Author: User
Tags soap stub validation examples

The previously used webservice were picked up as a result of the project, and Java native Jaxws, which did not require a jar package, was selected this time. First is the use of Wsimport, first share my usage: cmd, go to the project location first, and then run the following command

Copy Code code as follows:

Wsimport-keep-extension-s./src-p com.jaxws.test http://192.168.1.1:8080/service?wsdl

A variety of automatically generated client-related helper classes can be found under the Com.jaxws.test package. How to use these classes do not elaborate on the internet a lot of information. Then you write the calling class yourself (I'm just a method here and will be called on the line)

Copy Code code as follows:

Public String Jaxws (object[] opargs)
{
Servicesservice service=new Servicesservice ();

adding headers to Soap
Service.sethandlerresolver (New Handlerresolver () {
Public listlistAdd authentication information
Handlerlist.add (New ClientHandler ());
return handlerlist;
}
});

String result =service.getservicesport (). GetResults (Opargs.tostring ());
Get the results
SYSTEM.OUT.PRINTLN (result);
return result;
}

Note that "//Add authentication information Handlerlist.add (new ClientHandler ());", so we also need to create a new ClientHandler class to implement the authentication message assembly, as follows:

Copy Code code as follows:

Package com.jaxws.test;

Import Java.util.Set;
Import Javax.xml.namespace.QName;
Import javax.xml.soap.*;
Import Javax.xml.ws.handler.MessageContext;
Import Javax.xml.ws.handler.soap.SOAPHandler;
Import Javax.xml.ws.handler.soap.SOAPMessageContext;

public class ClientHandler implements soaphandler<soapmessagecontext> {

public boolean handlemessage (Soapmessagecontext ctx) {

Outbound, that is, before the client makes the request, add the header information
Boolean request_p= (Boolean) Ctx.get (Messagecontext.message_outbound_property);
if (request_p) {
try {
SoapMessage msg=ctx.getmessage ();
SoapEnvelope Env=msg.getsoappart (). Getenvelope ();
SoapHeader Hdr=env.getheader ();
if (hdr==null) Hdr=env.addheader ();

Add the authentication information header

QName (String NamespaceURI, String localpart, string prefix)
QName (String NamespaceURI, String localpart)
QName (String Localpart)
Namespace for @param namespaceuri:qname
@param the local part of Localpart:qname
Prefix for @param prefix:qname
QName name=new QName ("http://csdc.info/", "Authentication", "Wsse");
Soapheaderelement Header = hdr.addheaderelement (name);

Addchildelement (String localname, String prefix,string uri)
Addchildelement (string localname, string prefix)
Addchildelement (String localname)
@param uri: namespace name uri for new element
@param localname: The local name of the new element
@param prefix: space prefix for new element names
See the API for JDK 1.6
SoapElement userelement = header.addchildelement ("Username", "Wsse");
Userelement.addtextnode ("admin");
SoapElement passelement = header.addchildelement ("Password", "Wsse");
Passelement.addtextnode ("admin");

Msg.savechanges ();

Output SOAP messages to System.out, the console
Msg.writeto (System.out);

return true;
catch (Exception e) {
E.printstacktrace ();
}
}
return false;
}

@Override
public boolean Handlefault (Soapmessagecontext context) {
TODO auto-generated Method Stub
return false;
}

@Override
public void Close (Messagecontext context) {
TODO auto-generated Method Stub
}

@Override
Public set<qname> getheaders () {
TODO auto-generated Method Stub
return null;
}
}

This class adds a header message to all of the SOAP messages, and my header message here is as follows:
Copy Code code as follows:

<wsse:authentication xmlns:wsse= "http://csdc.info/" >
<wsse:Username>admin</wsse:Username>
<wsse:Password>admin</wsse:Password>
</wsse:Authentication>

This enables the JAXWS-based WebService client with SOAP header authentication.

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.