The simple use of webservice, the use of the CXF framework

Source: Internet
Author: User
Tags soap wsdl

Web Serviceis aPlatformIndependent, low-coupling, self-contained,Programmingof theWebapplication, you can use an openXML(Standard Universal Markup Languagea subset of the following)Standardcome toDescription, Publish, Discover, orchestrate, and configure these applications for the development of distributed, interoperableApplication.

The

Web service technology enables different applications running on different machines to exchange data or integrate with each other without the use of additional, specialized third-party software or hardware. Based on the application of web Service specification implementation, No matter what language, platform, or internal protocol they use, they can exchange data with each other. web Service web Service also very easy to deploy, Because they are based on some conventional industry standards and some of the existing technologies, such as Standard Universal Markup Language xml http web Service reduces the cost of application interfaces. web Service Provides a common mechanism for integration of business processes across the enterprise and even across multiple organizations.

The

Webservice webservice can make cross-language calls.

    1. WebService, as the name implies, is a Web-based service. It uses the Web (http\ XML) method to receive and respond to some kind of request from the external system. This allows for remote invocation .
    2. We can call the Internet to inquire about weather information Web Service, and then embed it in our program (c / s or /b) , when users see the weather information from our point of view, he will think we provide him with a lot of information services, But in fact, we did nothing, just a simple call to a piece of code on the server .
    3. Learning WebService can publish your service (a piece of code ) to the Internet for others to call , or to call the WebService published on someone else's machine , It's like using your own code.

I. Java code call WebService implementation of domestic mobile phone number attribution to the query service

First Open the Http://www.webxml.com.cn/zh_cn/index.aspx website: find domestic mobile phone number attribution to the query service

Go to Http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx website

Then go to the HTTP://WS.WEBXML.COM.CN/WEBSERVICES/MOBILECODEWS.ASMX?WSDL website

Then open the DOS command window on the desktop

Wsimport-s. -P com.baoyuan.ws http://ws.webxml.com.cn/webservices/mobilecodews.asmx?wsdl

Enter the code above-p to indicate that the package name can be defined by itself

Get the source code and CALSS files, you can delete the class file, leaving the source code

Then create the project and add the package in

Write the test code, find the name value of Wsdal:service in HTTP://WS.WEBXML.COM.CN/WEBSERVICES/MOBILECODEWS.ASMX?WSDL for the service class

The value of Wsdl:port found in HTTP://WS.WEBXML.COM.CN/WEBSERVICES/MOBILECODEWS.ASMX?WSDL is the proxy class, which can be obtained through the getter method of the service class.

The name value of the Wsdl:operation is the method, which is called through the proxy class

 Packagecom.baoyuan.test;ImportCom.baoyuan.ws.MobileCodeWS;ImportCom.baoyuan.ws.MobileCodeWSSoap; Public classTestWebService { Public Static voidMain (string[] args) {//instantiating a serviceMobilecodews ws =Newmobilecodews (); //Get proxy ObjectMobilecodewssoap soap =Ws.getmobilecodewssoap (); //calling methods through proxy objectsString info = soap.getmobilecodeinfo ("18662584581", "" ");    SYSTEM.OUT.PRINTLN (info); }}

Two. Publish the WebService service directly through the Java code of the JDK:

 PackageCom.baoyuan.server;ImportJavax.jws.WebService;ImportJavax.xml.ws.Endpoint;/*** Common one class implementation WebService Publishing Service * *@authorAdmin **/
Just add webservice annotations to the class
@WebService Public classHelloService { Publicstring Sayhell (string name) {returnName+ "is here."; } Public Static voidMain (string[] args) {//The Endpoint.publish method provided by the JDK will access the address: ip+ Port + Service name and service object incoming, publish serviceEndpoint.publish ("Http://localhost:8080/hello",NewHelloService ()); System.out.println ("Service started successfully"); }}

Integrate with the spring framework with the CXF framework to publish WebService

Create a Web project

Web. XML Configuration CXF

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "> <display-name>cxf_server</ display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file& Gt;index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!--configuration cxf servlet--> <servlet> <servlet-name >cxfServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class> <init-param> <param-name>config-location</param-name> <!--two ways classpath Web-inf-<!--Place the applicationcontext.xml in the src directory<param-value>classpath:applicationContext.xml</param-value> <param-value>/web-inf/appli cationcontext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet -name>cxfservlet</servlet-name> &LT;URL-PATTERN&GT;/CXF/*</url-pattern> </servlet-mapping></web-app>

Spring configuration file Applicationcontext.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jaxws= "Http://cxf.apache.org/jaxws"Xmlns:soap= "Http://cxf.apache.org/bindings/soap"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//Cxf.apache.org/bindings/soaphttp//cxf.apache.org/schemas/configuration/soap.xsdhttp//Cxf.apache.org/jaxwshttp//cxf.apache.org/schemas/jaxws.xsd "><!--Why use Applicationcontext.xml Spring and CXF consolidation to configure with one file-<!--way: Address to access Service addresses http://IP: Port/Service name Implementorclass the full path of the implementation class--<jaxws:endpoint address= "/moble" implementorclass= "Com.baoyuan.ws.service.impl.MobleAddressServiceImpl" > </jaxws:endpoint> <!--register an implementation class--<!--<bean id= "HelloService"class= "Cn.itcast.cxf.HelloServiceImpl" ></bean> <!--two: ID: If action or service is injected--<!--<j Axws:server address= "/hello" > <jaxws:serviceBean> <ref bean= "HelloService"/> </jaxws:servicebea N> </jaxws:server>--></beans>

Create a business interface to add WebService annotations to the class name

 PackageCom.baoyuan.ws.service;ImportJavax.jws.WebParam;ImportJavax.jws.WebService;/*** Simulation based on mobile phone number to search attribution *@authorAdministrator **///Just add WebService annotations to the interface@WebService Public InterfaceMobleaddressservice {//simulation According to the mobile phone number query attribution Webparam (name= "Mobleno") Note that the source code after the download of the name of the parameter or Mobleno will not change     PublicString getmobleaddress (@WebParam (name= "Mobleno") String Mobleno);}

To create an implementation class

 PackageCom.baoyuan.ws.service.impl;ImportCom.baoyuan.ws.service.MobleAddressService;/*** Simulation based on mobile phone number to search attribution *@authorAdministrator **/ Public classMobleaddressserviceimplImplementsmobleaddressservice{//simulation based on mobile phone number to query attribution@Override Publicstring getmobleaddress (String mobleno) {string result=mobleno+ "Place of ownership: Changsha"; returnresult; }}

Then access url:http://localhost:8080/cxf_server/cxf/moble?wsdl Comment: Cxf_server is the project name,/CXF is the path to the Access service configured in Web. XML,/ Moble for the address configured in the Spring configuration file

<wsdl:definitions xmlns:ns1= "http://service.ws.baoyuan.com/" xmlns:ns2= "Http://schemas.xmlsoap.org/soap/http" xmlns:soap= "http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns= "http://impl.service.ws.baoyuan.com/" xmlns:wsdl= " http://schemas.xmlsoap.org/wsdl/"xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "Name=" Mobleaddressserviceimplservice "targetnamespace=" http://impl.service.ws.baoyuan.com/"&GT;&LT;WSDL:Importlocation= "http://localhost:8080/cxf_server/cxf/moble?wsdl=MobleAddressService.wsdl" namespace= "http// service.ws.baoyuan.com/"&GT;&LT;/WSDL:Import><wsdl:binding name= "mobleaddressserviceimplservicesoapbinding" type= "Ns1:mobleaddressservice" ><soap : Binding style= "Document" transport= "Http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name= " Getmobleaddress "><soap:operation soapaction=" "style=" document "/><wsdl:input name=" getMobleAddress " ><soap:body use= "literal"/></wsdl:input><wsdl:output name= "Getmobleaddressresponse" >< Soap:body use= "literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service Name= "Mobleaddressserviceimplservice" ><wsdl:port binding= "tns:mobleaddressserviceimplservicesoapbinding" Name= "Mobleaddressserviceimplport" ><soap:address location= "http://localhost:8080/cxf_server/cxf/moble"/ ></wsdl:port></wsdl:service></wsdl:definitions>

Write client test code, download client code

Wsimport-s. -P com.baoyuan.ws http://localhost:8080/cxf_server/cxf/moble?wsdl

Create the project, copy and copy the downloaded client code, note: The code can be downloaded with-p to specify the package name, do not allow the download after the completion of the package name modification

Write test code, note: When testing: The server must be turned on

 Packagecom.baoyuan.ws.test;ImportCom.baoyuan.ws.MobleAddressService;ImportCom.baoyuan.ws.MobleAddressServiceImplService; Public classTESTCXF { Public Static voidMain (string[] args) {//instantiating a serviceMobleaddressserviceimplservice Service =NewMobleaddressserviceimplservice (); //Create a proxy objectMobleaddressservice Serviceimplport =Service.getmobleaddressserviceimplport (); //Calling MethodsString mobleaddress = serviceimplport.getmobleaddress ("13333532525");     System.out.println (mobleaddress); }}

The simple use of webservice, the use of the CXF framework

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.