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

CXF+Spring搭建WebService

时间:2014-11-05 14:43:37      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   使用   

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下。貌似包很多,不知道那些是多余的,干脆就直接用全部 。

结构:

bubuko.com,布布扣

接口

 

@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   显示像下图那样就说明搭建成功

bubuko.com,布布扣

 

以上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 。

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

然后就能看到

bubuko.com,布布扣

  然后就简单了。 代码

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

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

json得到了.

 

CXF+Spring搭建WebService

标签:des   style   blog   http   io   color   ar   os   使用   

原文地址:http://www.cnblogs.com/Marvellous/p/4076242.html

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