首页 > 代码库 > 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