What is a Web Service?
Web Service is not a frame. Not even a technology.
But a cross-platform, cross-language specification
what the Web Service solves:
to solve different platforms, how to invoke the problem between applications written in different languages. Like what. There is a program written in C language. It wants to invoke A method written in the Java language.
Centralized Solution: 1 , the remote call 2 . Cross-platform call 3, cross-language invocation
Practical application:
1 . The integration of the new and old systems of the same company. Java applications on Linux to invoke the C application of the Windows Platform
2 , the business integration of different companies. Business integration leads to system integration across different companies. Different company systems may have different platforms and different language issues
3 , content aggregation. An application, such as the need to provide, weather forecasts, stock quotes, gold quotes and so on.
The relationship between cxf and web Service
CXF is Apache's Open source framework, crafted by celtix+ Xfire, the classic framework. is a very popular Web service framework.
CXF Way to implement Web Service service demo
1, configuring environment variables
Add E:\apache-cxf-2.6.2\lib to Classpath
New cxf_homee:\apache-cxf-2.6.2
Add%cxf_home%\bin to Path
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>
2. developing Web service Development Server side using CXF
folder structure for the entire project (normal Java project):
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>
Interface:
Package Com.tgb.service;import javax.jws.WebService; @WebServicepublic interface Helloworld{public String Sayhi ( String name);}
Implementation class:
Package Com.tgb.service.impl;import Java.util.date;import Javax.jws.webservice;import com.tgb.service.HelloWorld;@ WebService (endpointinterface= "Com.tgb.service.HelloWorld", Servicename= "Helloworldimpl") public class Helloworldimpl implements Helloworld{public string Sayhi (string name) {return name+ "Hello!"Now the time is: "+new date ();}}
Test client:
Package Com.tgb.client;import Javax.xml.ws.endpoint;import Com.tgb.service.helloworld;import Com.tgb.service.impl.helloworldimpl;public class Servermain {public static void main (string[] args) {HelloWorld hw=new Helloworldimpl ();//Call Endpoint's Publish method to advertise the Web serviceendpoint.publish ("Http://192.168.24.215/hjy", HW); SYSTEM.OUT.PRINTLN ("Web service exposed successfully");}}
Start the program:
View WSDL
3, Develop WebServiceclient using CXF
run such as the following command (Find WebService working space of the client)
Refresh the client project to see the following generated code, for example:
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>
Write the client calling code:
Package Hjy;import Com.tgb.service.helloworld;import Com.tgb.service.impl.helloworldimpl;public class ClientMain { public static void Main (string[] args) {Helloworldimpl factory=new helloworldimpl ();//The only one returned here is the remote Web Service agent HelloWorld Hw=factory.gethelloworldimplport (); System.out.println (Hw.sayhi ("Hejingyuan"));}}
Operation Result:
Hejingyuan Hello! Now time: Tuejul 14:09:07 CST 2015
Summary:
Use CXF Development Web The service co-owns such as the following several steps:
1 , server-side
( 1 ) Develop a web Service business interface.
The interface is to be decorated with @webservice
( 2 ) Develop a web service Business implementation class. The implementation class also needs to be decorated with @webservice
( 3 ) Publish the Web Service
2 , Client
( 1 ) Call CXF provided by WSDL 2java tools, based on WSDL the document generates a corresponding Java code.
wsdl- Web Service Definition Language
no matter what language implements the Web Service, which is required and exposes a WSDL document
( 2 ) to find WSDL 2java in the generated class, one inherits the Service the class
Instances of this class can be used as a factory
( 3 ) Call Service instance of the subclass . Get Xxxport method. Returns the proxy for a remote Web service
Source code Download
Web Service Learning-CXF Development Web Service Instance demo (i)