码迷,mamicode.com
首页 > 编程语言 > 详细

CXF+Spring实现WebService

时间:2014-12-24 16:14:02      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

 

接口类:

import javax.jws.WebService;

@WebService
public interface CxfService {
    public String putName(String uname);
}

 

接口实现类:

import javax.jws.WebService;
import com.cxf.dao.CxfService;

@WebService
public 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

标签:

原文地址:http://www.cnblogs.com/heheyy/p/4182641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!