首页 > 代码库 > CXF+Spring实现WebService
CXF+Spring实现WebService
接口类:
import javax.jws.WebService;@WebServicepublic interface CxfService { public String putName(String uname);}
接口实现类:
import javax.jws.WebService;import com.cxf.dao.CxfService;@WebServicepublic class CxfServiceImpl implements CxfService { public String putName(String uname) { return "测试CXF-WebService:" + uname; }}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://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/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id="cxfService" implementor="com.cxf.dao.impll.CxfServiceImpl" address="/CxfService"> </jaxws:endpoint></beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> </web-app>
项目发布后访问http://localhost:8080/test/webservice/CxfService?wsdl可以看到:
测试类:
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import com.cxf.dao.CxfService;public class CxfTest { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(CxfService.class); factory.setAddress("http://localhost:8080/test/webservice/CxfService"); CxfService cxfService = (CxfService)factory.create(); System.out.println(cxfService.putName("测试")); }}
项目中用到的JAR包如下:
CXF+Spring实现WebService
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。