Axis2+springmvc detailed

Source: Internet
Author: User
Tags addchild gettext soap stub wsdl

Key code:

Maven Pom.xml

<axis2.version>1.6.2</axis2.version> <dependency><groupid>org.apache.axis2</groupid ><artifactid>axis2</artifactid><version>${axis2.version}</version><type>pom </type></dependency><dependency><groupId>org.apache.axis2</groupId>< artifactid>axis2-adb</artifactid><version>${axis2.version}</version><type>jar</ Type><scope>compile</scope></dependency><dependency><groupid>org.apache.axis2 </groupid><artifactid>axis2-kernel</artifactid><version>${axis2.version}</version ><type>jar</type><scope>compile</scope></dependency><dependency>< groupid>org.apache.axis2</groupid><artifactid>axis2-transport-http</artifactid>< Version>${axis2.version}</version></dependency><dependency><groupid> Org.apache.axis2</groupid><artifactId>axis2-transport-local</artifactid><version>${axis2.version}</version></dependency ><dependency><groupid>org.apache.axis2</groupid><artifactid>axis2-spring</ Artifactid><version>${axis2.version}</version></dependency>

  

Spring profile join (automatic scanning using annotations)

<bean id= "ApplicationContext" class= "Org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder"/ >

  

Xml

<servlet><servlet-name>AxisServlet</servlet-name><servlet-class> Org.apache.axis2.transport.http.axisservlet</servlet-class><init-param><param-name> contextconfiglocation</param-name><param-value>classpath:conf/axis2.xml</param-value></ Init-param><load-on-startup>2</load-on-startup> </servlet><servlet-mapping>< servlet-name>axisservlet</servlet-name><url-pattern>/services/*</url-pattern></ Servlet-mapping>

  

Java code

Public interface HelloService {public string SayHello (string name);}

  

Import Org.springframework.stereotype.Service; @Service ("HelloService") public class Helloserviceimpl implements HelloService {@Overridepublic string SayHello (string name) {//TODO auto-generated method stubif (name = = NULL) | | (Name = = "")) {            name = "Anonymous";        }        Return "Hello," + Name;}}

  

In Webcontent/web-inf/services/decode/meta-inf/services.xml

This path is built Services.xml file, decode can be named, and must be consistent

<?xml version= "1.0" encoding= "UTF-8"? ><servicegroup><service name= "HelloService" scope= "Application "><description>simple Spring example</description><module ref=" Usercheck "/>  <!-- Here for AXIS2 authentication-Use the module method to verify the SOAP header implementation slightly--><parameter name= "Serviceobjectsupplier" > org.apache.axis2.extensions.spring.receivers.springappcontextawareobjectsupplier</parameter>< Parameter Name= "Springbeanname" >helloservice</parameter><messagereceivers><messagereceiver MEP = "Http://www.w3.org/2004/08/wsdl/in-only" class = "Org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/> <messagereceiver mep= "http://www.w3.org/2004/08/wsdl/in-out" class = " Org.apache.axis2.rpc.receivers.RPCMessageReceiver "/></messagereceivers></service></ Servicegroup>

  

Start Project Browser input http://localhost:8080/project name/services/helloservice?wsdl you can see the WSDL information showing WebService, indicating that the deployment was successful.

One thing to note is that the location of the Services.xml file is fixed: web-inf/services/a folder of any name/meta-inf/services.xml

Client:

Import Javax.xml.namespace.qname;import Org.apache.axis2.axisfault;import Org.apache.axis2.addressing.endpointreference;import Org.apache.axis2.client.options;import Org.apache.axis2.rpc.client.rpcserviceclient;import Org.apache.axiom.om.omabstractfactory;import Org.apache.axiom.om.omelement;import Org.apache.axiom.om.omfactory;import Org.apache.axiom.om.omnamespace;public Class ServiceClient {public static void main (String args[]) throws Axisfault {SendAxis2 ();} /** * Add Header Header Verification information * * @return */public static omelement createheaderomelement () {Omfactory factory = omabstractfactory. Getomfactory (); Omnamespace securityelementnamespace = Factory.createomnamespace ("http://handler.com", "Wsse"); O Melement authenticationom = factory.createomelement ("Authentication", securityelementnamespace); OMElement Usernameom = Factory.createomelement ("username", securityelementnamespace); Omelement passwordom = Factory.createomelement ("password", securityelementnamespace), Usernameom.settext ("admin");p AsswordoM.settext ("123456"); Authenticationom.addchild (Usernameom); Authenticationom.addchild (PasswordOM); return Authenticationom;} /** * Send Axis2 interface information * @throws axisfault */private static void SendAxis2 () throws Axisfault {//RPC call Webservicerpcservic Eclient serviceclient = new Rpcserviceclient ();//add validation information to the SOAP header Serviceclient.addheader (Createheaderomelement () ); Options options = Serviceclient.getoptions ();//Specify Urlendpointreference TARGETEPR to call WebService = new EndpointReference ("http://localhost:8080/project name/services/helloservice"); Options.setto (TARGETEPR);// Specify the parameter value of the SayHello method, if there are multiple, continue to add to the back object[] Opaddentryargs = new object[] {"Axis2"};// Class object specifying the data type of the SayHello method return value class[] classes = new class[] {String.class};//when the QName object is created, the first parameter of the constructor method of the QName class represents the WSDL, The namespace name of the file, which is the targetnamespace attribute value of the <wsdl:definitions> element//The second parameter is the method name to invoke QName opaddentry = new QName ("HTTP/ Service.alfred.com "," SayHello ");//return parameter type, this and axis1 a little difference//Invokeblocking method has three parameters://The type of the first parameter is a QName object that represents the method name to invoke;// The second parameter represents the Webservi to invokeThe parameter value of the CE method, the parameter type is object[];//the third parameter represents the class object of the return value type of the WebService method, and the parameter type is class[];//when the method has no arguments. The second parameter value of the Invokeblocking method cannot be null, but to use new object[]{}//if the called WebService method has no return value, the Invokerobust method of the Rpcserviceclient class should be used. The method has only two parameters, and they have the same meaning as the first two parameters of the Invokeblocking method, object ret = serviceclient.invokeblocking (Opaddentry, Opaddentryargs, classes) [0]; SYSTEM.OUT.PRINTLN (ret);}}

  

Can be run

AXIS2 Authentication-Validate SOAP header implementations with module mode

Based on the identity authentication of business users. To achieve this, we need to do three things:

1. Realize the expansion module based on the identity authentication of business users,

2, load the extension module on the server side,

3. When the client calls, add the identity information to the SOAP header.


One, the realization of the business user-based Identity authentication extension Module
Need to create a new generic Java project, create a new two classes, one module is the other is handler. The module class must implement the Org.apache.axis2.modules.Module interface, without having to implement any interface methods specifically, unless your extension has initialization and destruction operations. This is required by the AXIS2 architecture.

1. New Usercheckmodule class

Import Org.apache.axis2.axisfault;import Org.apache.axis2.context.configurationcontext;import Org.apache.axis2.description.axisdescription;import Org.apache.axis2.description.axismodule;import Org.apache.axis2.modules.module;import Org.apache.neethi.assertion;import Org.apache.neethi.policy;public Class Usercheckmodule implements module {@Overridepublic void init (Configurationcontext configcontext, Axismodule module) Throws Axisfault {//TODO auto-generated method stub} @Overridepublic void Engagenotify (Axisdescription axisdescription) Throws Axisfault {//TODO auto-generated Method stub} @Overridepublic Boolean cansupportassertion (assertion assertion) {/ /TODO auto-generated method Stubreturn false;} @Overridepublic void ApplyPolicy (Policy policy, axisdescription axisdescription) throws Axisfault {//TODO auto-generated method stub} @Overridepublic void Shutdown (Configurationcontext configurationcontext) throws Axisfault { TODO auto-generated Method stub}}

  


The handler class must inherit the Org.apache.axis2.handlers.AbstractHandler base class. Implement the Invoke method, and the specific user authentication logic is implemented in this method.

2, Usercheckhandler class

Import Java.util.iterator;import Org.apache.axiom.om.omelement;import Org.apache.axis2.axisfault;import Org.apache.axis2.context.messagecontext;import Org.apache.axis2.engine.handler;import Org.apache.axis2.handlers.abstracthandler;public class Usercheckhandler extends Abstracthandler implements Handler {@ Override public invocationresponse Invoke (Messagecontext msgcontext) throws Axisfault {//Get head Omeleme        NT Fistelement = Msgcontext.getenvelope (). GetHeader (). Getfirstelement (); if (fistelement!=null&&fistelement.getchildren () = null) {iterator<?> list = (iterator<?>            ) Fistelement.getchildren ();            String Username = "";            String Password = "";                while (List.hasnext ()) {omelement element = (omelement) list.next ();                if (Element.getlocalname (). Equals ("username")) {username = Element.gettext (); } if (Element.getlocalname (). eqUals ("password")) {password = Element.gettext (); }} if (! Username.equals ("admin") | | ! Password.equals ("Password")) {throw new Axisfault ("Authentication fail!            Check Username/password ");        } return invocationresponse.continue; } else {throw new Axisfault ("Authentication fail!        Check Username/password "); }    }}

  


3. Next, we need to write module.xml to show axis2 what the implementation of our extended module is. This file requires us to create a new Meta-inf folder under the SRC directory and place this file under the Meta-inf folder.
In Module.xml, in addition to <InFlow>, there are <OutFlow>,<InFaultFlow> and <outfaultflow>, respectively, corresponding to the in, out, into error, error four processes, We can configure it as needed, as I do now, as long as the request comes in, so just configure <InFlow>.

<?xml version= "1.0" encoding= "UTF-8"? ><moudle name= "Usercheck" class= "package. Usercheckmodule" >    < inflow>        

4, at the end of the Axis2.xml <InFlow> add phase, like my this for <phase name= "Usercheckphase"/>

Axis2.xml references in the above Web. XML Introduction

5, modify the Services.xml file, in the need for identity authentication services to reference the Identity authentication module, <module ref= "Logging"/>. There are client calls in the above Services.xml file as shown above.

Axis2+springmvc detailed

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.