CXF+Spring实现WebService

 

接口类:

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包如下:

时间: 2024-08-28 00:09:54

CXF+Spring实现WebService的相关文章

webService总结(三)——使用CXF + Spring发布webService

近些年来,Spring一直很火,许多框架都能跟Spring完美集成,CXF也不例外.下面,我就介绍一下如何使用CXF + Spring发布webService.我们还是使用前两篇博客使用的实例. 服务端: 目录结构: 这里需要的所有Spring的包,CXF的lib目录下都有. IHelloWorldServer代码: package com.test.server; import javax.jws.WebService; @WebService public interface IHelloW

CXF+Spring搭建WebService

WebService: WebService 是一套标准,而不是一种具体的技术.不同的平台,不同的语言,大都提供了对 WebService 的开发实现. 从表面上看,Webservice 就是一个应用程序,它向外界暴露出一个能够通过 Web 进行调用的 API .也就是说,可以利用编程的方法通过 Web 来调用这个应用程序. 对 Webservice 更精确的解释 : Webservice 是建立可互操作的分布式应用程序的新平台.Webservice 平台是一套标准,它定义了应用程序如何在 We

cxf+spring发布webservice和调用

项目所需jar包:http://files.cnblogs.com/files/walk-the-Line/cxf-spirng.zip 首先写一个demo接口 package cn.cxf.demo; import javax.jws.WebService; @WebService public interface Demo { String sayHi(String text); } 然后就需要它的实现类 targetNamespace 是指向接口的包路径 package cn.cxf.de

CXF Spring开发WebService,基于SOAP和REST方式

版本CXF2.6.9 添加的包文件 这个版本的不可在Tomcat7上运行,会出错. 配置文件 applicationContext.xml [html] view plain copy <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://ww

CXF. Spring . Tomcat WebService

刚工作不就,公司需要用到webService , 就特意查了一下  : CXF的官网  http://cxf.apache.org/download.html   :  所需要的包都可以在里面找到 , cxf-2.3.3.jar geronimo-annotation_1.0_spec-1.1.1.jar geronimo-jaxws_2.2_spec-1.0.jar geronimo-stax-api_1.0_spec-1.0.1.jar geronimo-ws-metadata_2.0_sp

WebService CXF Spring

web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/ja

spring,cxf,restful发布webservice传递List,Map,List&lt;Map&gt;

上一篇文章中概述了怎么在Javaweb中发布webservice,这篇文章讲解怎么传递复杂的对象 所用的jar包如下 当服务器返回的是List或者是Map时,一定要将其封装在一个类中, 首先创建封装类,封装了List,Map对象,以及自定义的User类 User.java public class User { private String name; private int age; public User() { } public User(String name, int age) { t

webService总结(二)——使用CXF手动发布webService(不使用Spring)

上篇博客:webService总结(一)--使用CXF发布webService(不使用Spring) 介绍了不使用Spring自动发布webService,这篇博客介绍第二种方法--使用CXF手动发布webService(不使用Spring). CXF自动发布webService,我们使用的是Tomcat服务器.而使用CXF手动发布webService我们不再使用Tomcat,取而代之的是内嵌的jetty服务器.其实,jetty跟Tomcat并没有本质的区别,只是表现的形式不同,使用方法不同.既

httpclient4.x调用cxf发布的webservice的某个方法(有参数,有返回值)(未整合spring)

原文:httpclient4.x调用cxf发布的webservice的某个方法(有参数,有返回值)(未整合spring) 源代码下载地址:httpclient4.x调用cxf发布的webservice的某个方法(有参数,有返回值)(未整合spring) 采用spring + cxf编写服务端 httpclient4编写客户端调用 如题,代码没有jar 完整包: 链接:http://pan.baidu.com/share/link?shareid=2162612373&uk=402880896 密