cxf30集成笔记Word文档格式.docx
《cxf30集成笔记Word文档格式.docx》由会员分享,可在线阅读,更多相关《cxf30集成笔记Word文档格式.docx(18页珍藏版)》请在冰点文库上搜索。
@WebService(targetNamespace="
name="
HellowWebservice"
)
publicinterfaceHellowWebservice{
@WebMethod
@RequestWrapper(localName="
hellowRequest"
targetNamespace="
@ResponseWrapper(localName="
hellowResponse"
@WebResult(name="
return"
publicStringhellow(@WebParam(name="
name"
name);
}
这里使用注解配置webservice如果不配置,那么启动会异常
实现代码:
cys.webservice.impl.HellowWebserviceImpl
packagecys.webservice.impl;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importcys.webservice.HellowWebservice;
publicclassHellowWebserviceImplimplementsHellowWebservice{
publicStringhellow(Stringname){
returnnewSimpleDateFormat("
yyyy-MM-ddHH:
mm:
ss"
).format(newDate())+"
Hellow"
+name+"
这里是服务器。
"
;
}
先上web.xml配置:
这里贴出最小配置:
<
?
xmlversion="
1.0"
encoding="
ISO-8859-1"
>
web-appxmlns="
xmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
version="
2.4"
<
display-name>
cxftest<
/display-name>
description>
/description>
!
--ContextConfigurationlocationsforSpringXMLfiles-->
context-param>
<
param-name>
contextConfigLocation<
/param-name>
param-value>
/WEB-INF/applicationContext-*.xml<
/param-value>
/context-param>
listener>
listener-class>
org.springframework.web.context.ContextLoaderListener
/listener-class>
/listener>
welcome-file-list>
welcome-file>
login.jsp<
/welcome-file>
/welcome-file-list>
servlet>
servlet-name>
CXFService<
/servlet-name>
servlet-class>
org.apache.cxf.transport.servlet.CXFServlet<
/servlet-class>
load-on-startup>
3<
/load-on-startup>
/servlet>
servlet-mapping>
url-pattern>
/webservices/*<
/url-pattern>
/servlet-mapping>
/web-app>
再来一个spring的配置也是最小的只有CXF的配置:
WEB-INF/applicationContext-cxf.xml
UTF-8"
beansxmlns="
//www.springframework.org/schema/beans"
aop="
//www.springframework.org/schema/aop"
tx="
//www.springframework.org/schema/tx"
context="
//www.springframework.org/schema/context"
jaxws="
//cxf.apache.org/jaxws"
http:
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
//www.springframework.org/schema/txhttp:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
//www.springframework.org/schema/aophttp:
//www.springframework.org/schema/aop/spring-aop-2.5.xsd
//www.springframework.org/schema/contexthttp:
//www.springframework.org/schema/context/spring-context-2.5.xsd
//cxf.apache.org/jaxws
//cxf.apache.org/schemas/jaxws.xsd"
>
importresource="
classpath:
META-INF/cxf/cxf.xml"
/>
--这里是cxf2.x版本的配置,在3.0里已经没有了要去掉
importresource="
META-INF/cxf/cxf-extension-soap.xml"
-->
META-INF/cxf/cxf-servlet.xml"
--注意下面的address,这里的address的名称就是访问的WebService的name-->
--配置实现Bean-->
beanid="
hellowWebservice"
class="
cys.webservice.impl.HellowWebserviceImpl"
/bean>
--这里是webservice的发布配置,implementor="
#hellowWebservice"
中的#说明是从spring中取得Bean实例-->
jaxws:
endpointid="
implementor="
address="
/HellowWebservice"
/beans>
OA,说说依赖jar包的事。
CXF3.0.2从官网下下载下来可以看到其jar包,已经按照功能划分了许多的小jar包,可以根据需要加载。
这里把cxf和其依赖的jar包文件全部都考过去,在我真实的项目中一开始是将cxf向我项目中移植加载,但是总是发现有些jar包上的冲突。
导致一些不可预知的情况,启动不正常或者在调用的时候由于jar包的版本等原因出现问题。
后来反其道而行之,先将cxf的环境搭好,然后将我的项目jar包向可运行的cxf环境中移植。
最后spring使用了3.2.12版本。
可以正常运行。
部署并启动完毕后访问以下地址:
//127.0.0.1/cxftest/webservices/
可以看到已经发布了HellowWebservice
点击进入查看此webservice的wsdl描述:
//127.0.0.1/cxftest/webservices/HellowWebservice?
wsdl
xmlversion='
1.0'
encoding='
UTF-8'
wsdl:
definitionsxmlns:
xsd="
//www.w3.org/2001/XMLSchema"
xmlns:
wsdl="
//schemas.xmlsoap.org/wsdl/"
tns="
//impl.webservice.cys/"
soap="
//schemas.xmlsoap.org/wsdl/soap/"
ns2="
//schemas.xmlsoap.org/soap/http"
ns1="
importlocation="
wsdl=HellowWebservice.wsdl"
namespace="
/wsdl:
import>
bindingname="
HellowWebserviceImplServiceSoapBinding"
type="
ns1:
soap:
bindingstyle="
document"
transport="
operationname="
hellow"
operationsoapAction="
style="
inputname="
bodyuse="
literal"
input>
outputname="
output>
operation>
binding>
servicename="
HellowWebserviceImplService"
portbinding="
tns:
name="
HellowWebserviceImplPort"
addresslocation="
//127.0.0.1/cxftest/webservices/HellowWebservice"
port>
service>
definitions>
这里看到WSDL但是仔细看这个WSDL,其描述不完整,例如没有hellow方法的出参、入参描述,咋办?
查了查文档,完整的WSDL参数等描述的查看地址是:
wsdl=HellowWebservice.wsdl
types>
xs:
schemaxmlns:
xs="
elementname="
hiRequest"
hiResponse"
complexTypename="
sequence>
elementform="
qualified"
minOccurs="
0"
string"
/xs:
complexType>
person"
elementminOccurs="
id"
schema>
messagename="
hi"
partelement="
parameters"
part>
message>
portTypename="
inputmessage="
outputmessage="
portType>
这里还有个小插曲:
jaxb-api的jar包,CXF3.0依赖的是jaxb-api-2.2.11.jar,但是jdk1.6.20以下自带的版本比较低,因此在运行的时候报了异常,不能正常加载class,这里两个解决方案,如果不升级JDK那么将:
CXF3.0.2版本中自带的endorsed,integration考到使用的JDK或者jre的lib目录下:
例如我的目录是:
C:
\ProgramFiles\Java\jdk1.6.0_11\jre\lib
第二方法,那么就是升级JDK了,这没有什么可说。
现在写个Client代码调用一下看看:
packagecom.test.client;
importjava.lang.reflect.InvocationTargetException;
importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;
publicclassTestClient{
publicstaticvoidmain(String[]args)throwsInstantiationException,IllegalAccessException,ClassNotFoundException,SecurityException,NoSuchMethodException,IllegalArgumentException,InvocationTargetException{
JaxWsProxyFactoryBeanfactory=newJaxWsProxyFactoryBean();
factory.setServiceClass(HellowWebservice.class);
factory.setAddress("
);
HellowWebserviceservice=(HellowWebservice)factory.create();
Stringresult=service.hellow("
张三"
System.out.println("
result"
+result);
这个是使用其RMI的调用风格调用,感觉很好,远程调用要类似hessian的部署将需要接口class文件部署过去。
返回结果:
扩展下webservie接口,