The premise of this article is that there is already a spring project, on this basis how to combine with AXIS2, develop WebService service and invoke WebService service.
1. Open WebService Service
1. Introduce the necessary jar packages
Bring all the axis2-1.6.2-bin\axis2-1.6.2\lib packages into your own project. (Of course, there are some unnecessary, interested can cut their own).
2. Introduction of necessary documents and creation of new Service.xml
1. Copy the Conf directory in the \axis2-1.6.2-war\axis2\web-inf, modules directory, to the Web-inf of your project.
2. In Web-inf, create a new services directory (must), which can be divided into specific subdirectories, sub-directories under the creation of Meta-inf directory (must), the directory under the new file: Services.xml (MUST),
After the creation is complete, the directory structure is as follows:
The contents of the Service.xml are as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Servicename= "TestWebService"> <Description>TestWebService</Description> <parametername= "Serviceobjectsupplier">Org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter> <parametername= "Springbeanname">TestWebService</parameter> <messagereceivers> <MessagereceiverMEP= "Http://www.w3.org/2004/08/wsdl/in-only"class= "Org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> <MessagereceiverMEP= "Http://www.w3.org/2004/08/wsdl/in-out"class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </messagereceivers> <SchemaSchemanamespace= "http://service.telchina.cn" /></Service>
3. Modify Web. XML to add the following configuration
<servlet> <Servlet-name>Axisservlet</Servlet-name> <Servlet-class>Org.apache.axis2.transport.http.AxisServlet</Servlet-class> <Load-on-startup>2</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Axisservlet</Servlet-name> <Url-pattern>/services/*</Url-pattern> </servlet-mapping>
4. Modify Applicationcontext.xml
<id= "ApplicationContext" class= " Org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder " />
Ps:
This configuration must be added, no error:
caused By:java.lang.Exception:Axis2 Can ' t find Spring ' s ApplicationContext
Official Description: https://axis.apache.org/axis2/java/core/docs/spring.html
5. Create a service class
Package Cn.telchina.standard.service; Import org.springframework.stereotype.Component; @Component ("TestWebService") Public class TestWebService { public string SayHello (string name) { return ] Hello "+name; }}
2. Call the WebService service
There are two ways of calling:
1.RPC mode
Public voidtestrpcclient () {Try { //Axis2 Service SideString url = "Http://localhost:8080/axis2Project/services/testWebService"; //call WebService using RPC modeRpcserviceclient serviceclient =Newrpcserviceclient (); //Specifies the URL to call WebServiceEndpointReference Targetepr =Newendpointreference (URL); Options Options=serviceclient.getoptions (); //Determine destination service addressOptions.setto (TARGETEPR); /*** Specify the GetPrice method to invoke and the namespace of the WSDL file * If the WebService service driven by Axis2 write the problem caused by a namespace inconsistency * org.apache.axis2.AxisFault:java.lang.RuntimeException:Unexpected subelement arg0*/QName QName=NewQName ("http://service.telchina.cn", "SayHello"); //specifying the parameter value of the GetPrice methodobject[] Parameters =NewObject[] {"Name" }; //class object specifying the data type of the GetPrice method return valueclass[] Returntypes =NewClass[] {String.class }; //call method One pass parameter, invoke service, get service return result setomelement element =serviceclient.invokeblocking (QName, parameters); //It is worth noting that the returned result is an XML string encapsulated by the Omelement object. //we can apply it flexibly, and below I take the first element value and print it. Because the called method returns a resultString result =element.getfirstelement (). GetText (); SYSTEM.OUT.PRINTLN (result); // //Call method Two GetPrice method and output The return value of the method//object[] response = serviceclient.invokeblocking (QName, parameters, returntypes); // //String r = (string) response[0]; //String r = (string) response[0]; //System.out.println (R); } Catch(Axisfault e) {e.printstacktrace (); } }
2.AXIOM mode
/*** Method Two: Application of document mode call * Using Ducument method is cumbersome and flexible. It's more used now. Because we really get rid of the coupling we don't want.*/ Public voidtestdocument () {Try { //String url = "http://localhost: 8080/axis2serverdemo/services/stockquoteservice "; String url = "Http://localhost:8080/axis2Project/services/testWebService"; Options Options=NewOptions (); //Specifies the URL to call WebServiceEndpointReference Targetepr =Newendpointreference (URL); Options.setto (TARGETEPR); //options.setaction ("Urn:getprice"); serviceclient Sender=Newserviceclient (); Sender.setoptions (options); Omfactory FAC=omabstractfactory.getomfactory (); //Namespaces , and sometimes namespaces don't add up okay, but better to add, because sometimes things happen, you know.Omnamespace omns = Fac.createomnamespace ("http://service.telchina.cn", "" "); Omelement Method= Fac.createomelement ("SayHello", Omns); Omelement symbol= Fac.createomelement ("name", Omns); //Symbol.settext ("1"); Symbol.addchild (Fac.createomtext (symbol, "Axis2 Echo String")); Method.addchild (symbol); Method.build (); Omelement result=sender.sendreceive (method); SYSTEM.OUT.PRINTLN (result); } Catch(Axisfault axisfault) {axisfault.printstacktrace (); } }
Ps:
Although this example uses spring annotations to declare the bean, AXIS2 itself does not support the use of @webservice annotations to directly declare the service, which is fundamentally different from CXF.
Enter the address in the browser: http://localhost:8080/axis2Project/services/testWebService?wsdl
can see:
Reference article:
1.http://www.cnblogs.com/linjiqin/archive/2011/07/05/2098316.html
2.http://sunpfsj.blog.163.com/blog/static/1770500972013424113314769/
Spring Project combined with AXIS2