WebService小例子

阅读更多
刚到新公司,发现项目中有webservice的调用,而且不少,正好趁此机会学习一下。
下面是一个简单的例子,利用MyEclipse自动生成了很多文件,因此深入的话可能不是那么了解,不过先对webservice有个大致的认识吧。
How to?
step 1
New a WebService Project
give a project name
choose Framework as XFire 
warning
If we choose the j2ee5.0 for specification.The tomcat5 must not be used.
Because of jars conflict.
click next to project library configuration
select nothing in the area.we have to download a new version XFire jar for our project.
put jar in the 'lib' folder.remember to config the classPath.
now.create packages and create interface.of course.some methods should be created in this interface.
next.create a class to implement the interface above.
finish your method in the class.
new a webService for this project
select 'create web service from java bean' for strategy
click next
give a name to the web service.Be sure the path of interface and the path of imolement class are correctly.
then finish the config.
Let's have a try
http://127.0.0.1:8080//services/?wsdl
If we see the content like xml file in our exploror.
congratulations  we have finished the step 1.
What is wsdl?
| |
Web Service Describe Language.
step 2
Create a java project .
create packages for java files...
new a web service client for this project.
select 'SW-JAX' for framework.
write 'http://127.0.0.1:8080//services/?wsdl'in to the WSDL URL textarea.
put the path of the package we've created into the 'Package' textarea.
click next .after testing...finish.
If no errors.we could create a class with a main method.
and write :
MyEclipse will create some class flies autoly...
PortType service = new ().getHttpPort();
then we can use the 'service' we created to call any method in the web service project above.

以上纯手写,肯定有不对的地方,估计就自己能看懂。
web service 其实就是一个跨语言,跨平台,跨各种的交流通道,A是C#项目,B是java项目,A想要调用B中的方法,那么B先创造WS所需要的条件,暴露出可以供A调用的接口,A通过WSClient调用B所暴露给自己的接口即实现了调用B项目中的方法。不同的语言怎么能相互调用?大家交流的关键就是xml文件。说的低俗点就是有一个叫xml的翻译会各国语言,通过他,几个只会本国语言的老头谈天说地。
当然执行这个并非随意之事,WS是遵守着规范的。

你可能感兴趣的:(JAVA)