Java通过Cxf创建webService接口.docx
《Java通过Cxf创建webService接口.docx》由会员分享,可在线阅读,更多相关《Java通过Cxf创建webService接口.docx(18页珍藏版)》请在冰豆网上搜索。
![Java通过Cxf创建webService接口.docx](https://file1.bdocx.com/fileroot1/2022-11/16/d98c50f0-3392-4ff4-9271-726eededfb69/d98c50f0-3392-4ff4-9271-726eededfb691.gif)
Java通过Cxf创建webService接口
Java通过Cxf创建webService接口
一、导入cxf需要的jar包
二、编写服务类
publicclassCxfServiceimplementsCxfServiceImpl{
/*(non-Javadoc)
*@see.liveuc.cxf.service.CxfServiceImpl#getSay(java.lang.String)
*/
@WebMethod
publicStringgetSay(Stringname){
return"say:
"+name+"时间:
"+newDate();
}
}
@WebService
publicclassCxfServiceimplementsCxfServiceImpl{
/*(non-Javadoc)
*@see.liveuc.cxf.service.CxfServiceImpl#getSay(java.lang.String)
*/
@WebMethod
publicStringgetSay(Stringname){
return"say:
"+name+"时间:
"+newDate();
}
}
三、抽取接口
publicinterfaceCxfServiceImpl{
@WebMethod
publicabstractStringgetSay(Stringname);
}
@WebService
publicinterfaceCxfServiceImpl{
@WebMethod
publicabstractStringgetSay(Stringname);
}
四、写启动类
publicclassStartService{
publicStartService(){
//实例化服务类
CxfServicecs=newCxfService();
//用ServerFactoryBean发布服务
JaxWsServerFactoryBeansf=newJaxWsServerFactoryBean();
//设置服务访问地址
sf.setAddress("http:
//127.0.0.1:
9090/say");
//设置服务接口类
sf.setServiceClass(CxfServiceImpl.class);
//设置服务具体类
sf.setServiceBean(cs);
//开启服务
sf.create();
}
publicstaticvoidmain(String[]args){
newStartService();
}
}
二、cxf与Spring集成webservice
二、编写Spring配置文件applicationContext.xml
?
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
jaxws="http:
//cxf.apache.org/jaxws"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans.xsd
http:
//cxf.apache.org/jaxwshttp:
//cxf.apache.org/schemas/jaxws.xsd">
rgb(255,255,255)">#ff0000">--
***注意***
手动添加的内容:
xmlns:
jaxws="http:
//cxf.apache.org/jaxws"
http:
//cxf.apache.org/jaxwshttp:
//cxf.apache.org/schemas/jaxws.xsd"
-->
--引入CXFBean定义如下-->
META-INF/cxf/cxf.xml"/>
META-INF/cxf/cxf-extension-soap.xml"/>
META-INF/cxf/cxf-servlet.xml"/>
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
jaxws="http:
//cxf.apache.org/jaxws"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans.xsd
http:
//cxf.apache.org/jaxwshttp:
//cxf.apache.org/schemas/jaxws.xsd">
--***注意***
手动添加的内容:
xmlns:
jaxws="http:
//cxf.apache.org/jaxws"
http:
//cxf.apache.org/jaxwshttp:
//cxf.apache.org/schemas/jaxws.xsd"
-->
--引入CXFBean定义如下-->
META-INF/cxf/cxf.xml"/>
META-INF/cxf/cxf-extension-soap.xml"/>
META-INF/cxf/cxf-servlet.xml"/>
三、在web.xml中配置Spring的加载文件和cxf拦截器
--加载Spring配置文件-->
contextConfigLocation
classpath:
applicationContext.xml
org.springframework.web.context.ContextLoaderListener
--配置CXF配置文件-->
cxf
org.apache.cxf.transport.servlet.CXFServlet
1
cxf
/ws/*
--所有的webservice发布在ws目录下-->
--加载Spring配置文件-->
contextConfigLocation
classpath:
applicationContext.xml
org.springframework.web.context.ContextLoaderListener
--配置CXF配置文件-->
cxf
org.apache.cxf.transport.servlet.CXFServlet
1
cxf
/ws/*
--所有的webservice发布在ws目录下-->
四、编写要提供的webservice接口和实现类
2.编写服务接口
@WebService
publicinterfaceCxfServiceImpl{
@WebMethod
publicabstractStringgetSay(Stringname);
}
3.编写服务具体类
@WebService
publicclassCxfServiceimplementsCxfServiceImpl{
/*(non-Javadoc)
*@see.liveuc.cxf.service.CxfServiceImpl#getSay(java.lang.String)
*/
@WebMethod
publicStringgetSay(Stringname){
return"say:
"+name+"时间:
"+newDate();
}
}
2.编写服务接口
@WebService
publicinterfaceCxfServiceImpl{
@WebMethod
publicabstractStringgetSay(Stringname);
}
3.编写服务具体类
@WebService
publicclassCxfServiceimplementsCxfServiceImpl{
/*(non-Javadoc)
*@seecn.