CXF+Spring搭建WebService

WebService:

  WebService 是一套标准,而不是一种具体的技术。不同的平台,不同的语言,大都提供了对 WebService 的开发实现。

从表面上看,Webservice 就是一个应用程序,它向外界暴露出一个能够通过 Web 进行调用的 API 。也就是说,可以利用编程的方法通过 Web 来调用这个应用程序。

对 Webservice 更精确的解释 : Webservice
是建立可互操作的分布式应用程序的新平台。Webservice 平台是一套标准,它定义了应用程序如何在 Web 上实现互操作性。

你可以用任何你喜欢的语言,在任何你喜欢的平台上写 Webservice ,只要我们可以通过
Webservice 标准对这些服务进行查询和访问。

不管你的 Webservice 是用什么工具,什么语言写出来的,只要你用 SOAP
协议通过 HTTP 来调用它,总体结构都一致。通常,你用你自己喜欢的语言(如VB 6或者VB.NET)

来构建你的
Webservice,然后用 SOAP Toolkit 或者 .NET 的内建支持来把它暴露给 Web
客户。于是,任何语言,任何平台上的客户都可以阅读其WSDL文档,

以调用这个 Webservice。客户根据 WSDL 描述文档,会生成一个 SOAP
请求消息。Webservice 都是放在 Web 服务器 (如IIS) 后面的,客户生成的 SOAP 请求

会被嵌入在一个
HTTP POST 请求中,发送到 Web 服务器来。Web 服务器再把这些请求转发给 Webservice 请求处理器。请求处理器的作用在于,解析收到的
SOAP 请求,

调用 Webservice,然后再生成相应的 SOAP 应答。Web 服务器得到 SOAP
应答后,会再通过 HTTP 应答的方式把它送回到客户端。   "

以上来自网络。

----------------------------------------------------------------------

搭建CXF和Spring整合的WebService:

环境:

java7

CXF 2.7.13

CXF 包里边有带Spring.30 直接用

新建一个web项目名称test,cxf下面lib文件里的所有包copy到项目的lib下。貌似包很多,不知道那些是多余的,干脆就直接用全部 。

结构:

接口

@WebService
public interface OutService {

    public String getBinSerial(@WebParam(name="text") String text);

}

实现类

//@WebService(endpointInterface="com.xxx.xxx") 客户端和服务端包名不同的时候要加endpointInterface访问
@WebService
@Component("outServiceImpl")
public class OutServiceImpl implements OutService{

    public String getBinSerial(String text) {

        JSONObject jo = new JSONObject();
        jo.put("KK", 12);
        jo.put("WW", "WW");
        return jo.toString();
    }

}

这里的Component 注解 是spring  annotation  相当于在applicationContext.xml中的

<bean  id ="outServiceImpl" class="com.xxxx.."  />

下面是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:p="http://www.springframework.org/schema/p"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:cxf="http://cxf.apache.org/core"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://cxf.apache.org/jaxws
              http://cxf.apache.org/schemas/jaxws.xsd">

       <context:annotation-config/>
    <context:component-scan base-package="com.tek.ks" />

       <!--  cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml放在 cxf-2.5.3.jar 里面的 META-INF 文件夹的 cxf 目录下 -->
       <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" />  

    <!-- JAX-WS -->
    <!-- implementor 指定 WebService 实现类, address 指定访问地址 -->
    <jaxws:endpoint id="helloWorld" implementor="#outServiceImpl" address="/outService" />  

</beans>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    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_3_0.xsd">

   <!-- Spring 配置  -->
   <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>

  <!-- CXF 配置     -->
     <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>/*</url-pattern>
    </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

到这里搭建完成,部署到tomcat

在浏览器地址栏访问 : http://localhost:8080/test/outService?wsdl   显示像下图那样就说明搭建成功

以上CXF+Spring  服务端搭建完成。

以下是客户端,

    public static void main(String[] args) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        // 注册WebService接口
        factory.setServiceClass(OutService.class);
        // 设置WebService地址
        factory.setAddress("http://localhost:8080/test/outService");
        OutService os = (OutService) factory.create();
        String msg=os.getBinSerial("ssss");
        System.out.println(msg);
    }  

输出 打印json:{"KK":12,"WW":"WW"}

项目需求要使用C#调用CXF的webService

方法如下:

我使用的是vs2008 , 新建项目 , 在项目上右击-添加服引用-在地址中输入http://localhost:8080/test/outService?wsdl也就是之前测试服务端的地址

点击前往,在服务里就能查找到服务。然后 点击OK 。

这里要注意。 点击前往 查找服务的时候,服务端不能关掉的,这点不难理解吧。

然后就能看到

  然后就简单了。 代码

            ServiceReference1.OutServiceClient   osc = new WindowsFormsApplication1.ServiceReference1.OutServiceClient ();
            string json = osc.getBinSerial("sss");
            Console.WriteLine(json);

打印: {"KK":12,"WW":"WW"}

json得到了.

时间: 2024-08-24 07:58:03

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服务

虽然下一个项目需要使用xfire,但是在查资料的过程中还是看到有不少地方都说cxf比xfire更好,cxf继承了xfire,但是不仅仅包含xfire,因此便也一起来尝试尝试.大概是有了xfire的经验吧,cxf的搭建比xfire快了许多. cxf的许多参数感觉和xfire差不多,因此便不做太多的解释,如果不明白的可以参考之前的xfire搭建来促进理解. 搭建过程如下: 1.使用eclipse创建一个maven项目,maven导包的pom.xml的整体内容如下: <project xmlns=&quo

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

  接口类: 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 { pu

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

Apache CXF+Spring开发环境搭建小试

最近手上一个项目要开发webservice,而原有系统使用了spring,所以在选择框架的时候,我选择了cxf,这样在开发整合的时候就比较方便了.在搭建开发环境的过程中发现这篇文章写得比较详细,所以就搬到自己博客里,希望给自己和同行做参考. CXF 应用开发 下面就将开始我们的 CXF Web Services 的开发之旅!首先,要有一个基于 Eclipse 的开发环境:然后,我们将利用这个开发环境开发一个简单的“调查投票”示例,同时我们将解释一些 CXF 在开发中进行配置的基本方法. 开发环境

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