最近一段时间研究webservice,一般来说,开发java的Webservice经常使用axis2和cxf这两个比较流行的框架
先使用cxf,开发一个完整示例,方便对webservice有一个整体的概念
使用的工具主要有eclipse、maven
一、开发服务端
整体结构如下:
1、创建maven的web工程,这个就不多赘述了,如果以前没搭建过可以去网上搜索下,网上资源很多
2、配置POM.xml文件,引入相应的jar包
4.0.0
com.moon.ws
cfxWSServer
war
0.0.1-SNAPSHOT
cfxWSServer Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
org.apache.cxf
cxf-api
2.5.0
org.apache.cxf
cxf-rt-frontend-jaxws
2.5.0
org.apache.cxf
cxf-rt-bindings-soap
2.5.0
org.apache.cxf
cxf-rt-transports-http
2.5.0
org.apache.cxf
cxf-rt-ws-security
2.5.0
cfxWSServer
3、开发相关的接口和实现类
首先开发接口类
packagecom.moon.cxfWebservice.server;importjavax.jws.WebParam;importjavax.jws.WebService;
@WebServicepublic interfaceGreeting {public String greeting(@WebParam(name="username")String userName);
}
然后开发实现类
packagecom.moon.cxfWebservice.server;importjava.util.Calendar;importjavax.jws.WebService;
@WebService(endpointInterface="com.moon.cxfWebservice.server.Greeting")public class GreetingImpl implementsGreeting{publicString greeting(String userName) {return "Hello " + userName + ", currentTime is "
+Calendar.getInstance().getTime();
}
}
至此,服务端的代码就开发完成了。
4、配置web.xml和spring配置文件
web.xml配置如下
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
contextConfigLocation
/WEB-INF/config/spring.xml
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/webservice/*
hello world!
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
spring的配置文件
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
例如我本地的项目访问的就是
http://localhost:8080/cxfWSServer/webservice/Greeting?wsdl
二、开发客户端
客户端调用我使用的两种方式
第一种使用apche cxf生成代码进行访问
1、下载apache cxf的包,地址为:http://cxf.apache.org/download.html 如:apache-cxf-3.1.6
2、解压apache-cxf-3.1.6到任意目录
3、配置环境变量
os系统设置
1)、export CXF_HOME=/Users/moon/Desktop/tools/apache-cxf-3.1.6
2)、path后面加 :$CXF_HOME/bin
windows系统设置
1)、CXF_HOME=D:\apache-cxf-3.1.6
2)、在path后面加上 %CXF_HOME%/bin;
在命令中输入wsdl2java,如果有提示usage,就表明配置成功
4、运行wsdl2java工具
在命令中输入:wsdl2java -d \xx\xxx\xx -client http://localhost:8080/cxfWSServer/webservice/Greeting?wsdl
(\xx\xxx\xx 是客户端程序代码所在的目录,http://localhost:8080/cxfWSServer/webservice/Greeting?wsdl 是发布的webservice服务)
附wsdl2java用法:
wsdl2java -p com -d D:\\src -all xx.wsdl
-p 指定其wsdl的命名空间,也就是要生成代码的包名:
-d 指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
生成后的代码直接放到client工程上面
另外新建一个client类 直接使用生成的类调用
packagecom.moon.cxf;importcom.moon.cxf.client.Greeting;importcom.moon.cxf.client.GreetingImplService;public classCxfClient {public static voidmain(String[] args) {
GreetingImplService serviceFactory= newGreetingImplService();
Greeting service=serviceFactory.getGreetingImplPort();
String result= service.greeting("Jaune");
System.out.println(result);
}
}
二、使用axis调用webservice接口
引入axis 相关jar包
代码如下
packagecom.moon.cxf;importjava.rmi.RemoteException;importjavax.xml.namespace.QName;importjavax.xml.rpc.ParameterMode;importjavax.xml.rpc.ServiceException;importorg.apache.axis.client.Call;importorg.apache.axis.client.Service;importorg.apache.axis.encoding.XMLType;/*** 使用axis调用cxf发布的webservice接口
*@authormoon
**/
public classAxisClient {public static void main(String[] args) throwsServiceException, RemoteException {
try{
String endpoint= " http://localhost:8080/cfxWSServer/webservice/Greeting";//调用过程
Service service = newService();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(newjava.net.URL(endpoint));
call.setOperationName(new QName("http://server.cxfWebservice.moon.com/","greeting"));//WSDL里面描述的操作名称
call.addParameter("username",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);//操作的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
call.setUseSOAPAction(true);//给方法传递参数,并且调用方法
String temp = "good";
Object[] obj= newObject[] { temp };
String result=(String) call.invoke(obj);
System.out.println("Result is : " +result);
}catch(Exception e) {
e.printStackTrace();
}
}
}
相关代码:https://github.com/15210448683/WebServiceDemoImpl