`
weir2009
  • 浏览: 263578 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类
最新评论

webservice cxf spring 集成知识

    博客分类:
  • cxf
阅读更多



 Soa(
面向服务的架构)

  Service1Service2Service3…——所有组件都是“即插即用”的。

  IBM提倡的soa架构:希望以“组装电脑”的方式来开发软件。

1.       各种提供服务的组件。(web service

2.       企业服务总线(enterprise service busESB

CXF可称得上是SOA架构

Cxf内置了一个jetty web服务器

 

使用cxf开发web service服务器端:

Cxf环境变量设置:

ClasspathF:\apache-cxf-2.1.2\lib

CXF_HOME = "CXF安装路径". 例如:F:\apache-cxf-2.1.2

PATH中添加%CXF_HOME%/bin

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(1)       开发一个web service业务接口

该接口要用@webservice注解

 

 

package org.weir.cxf.ws;

import javax.jws.WebService;

@WebService

publicinterface Helloworld {

   String sayHi(String name);

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(2)       开发一个web service实现类

也需要@webservice注解

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package org.weir.cxf.ws.impl;

import javax.jws.WebService;

import org.weir.cxf.ws.Helloworld;

@WebService(endpointInterface="org.weir.cxf.ws.Helloworld",serviceName="HelloworldWs")

publicclass HelloworldWs implements Helloworld {

   @Override

   public String sayHi(String name) {

      return name+"HI";

   }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3)发布webservice

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package lee;

import javax.xml.ws.Endpoint;

import org.weir.cxf.ws.Helloworld;

import org.weir.cxf.ws.impl.HelloworldWs;

publicclass ServerMain {

   publicstaticvoid main(String[] args) {

      Helloworld hw = new HelloworldWs();

      //发布web service

   Endpoint.publish("http://172.168.1.172/weir", hw);

      System.out.println("Ko");

   }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

运行main方法不要结束

http://172.168.1.172/weir?wsdl

 

会出现:

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.ws.cxf.weir.org/"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://ws.cxf.weir.org/" name="HelloworldWs"targetNamespace="http://impl.ws.cxf.weir.org/">

<wsdl:import location="http://172.168.1.172/weir?wsdl=Helloworld.wsdl" namespace="http://ws.cxf.weir.org/"></wsdl:import>

<wsdl:binding name="HelloworldWsSoapBinding" type="ns1:Helloworld">

<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

<wsdl:operation name="sayHi">

<soap:operation soapAction="" style="document"/>

<wsdl:input name="sayHi">

<soap:body use="literal"/>

</wsdl:input>

<wsdl:output name="sayHiResponse">

<soap:body use="literal"/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name="HelloworldWs">

<wsdl:port binding="tns:HelloworldWsSoapBinding" name="HelloworldWsPort">

<soap:address location="http://172.168.1.172/weir"/>

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

 

 

 

客户端

 

(1)       调用cxf提供的wsdl2java工具,根据wsdl文档生成相应的java代码。

(任何语言实现了web service,都需要提供、并暴露wsdl文档)

WSDL——web service definition language

       cmd进入客户端的src目录:

      

运行 wsdl2java http://172.168.1.172/weir?wsdl

wsdl2java –encoding utf-8 http://172.168.1.172/weir?wsdl

 

 

 

 

 

 

(2)       找到wsdl2java所生成类中,一个继承了service的类。

该类的实例可当成工厂来使用

3)调用service子类的实例的getxxxPort方法,返回远程web service的代理。

 

package lee;

import org.weir.cxf.ws.Helloworld;

import org.weir.cxf.ws.impl.HelloworldWs;

publicclass ClientMain {

   publicstaticvoid main(String[] args) {

      HelloworldWs factory = new HelloworldWs();

      Helloworld hw = factory.getHelloworldWsPort();

      System.out.println(hw.sayHi("weir"));

   }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

形参、返回值

1.       当形参、返回值的类型是string、基本数据类型时,cxf可以处理

2.       当形参、返回值的类型是javabean式的符合类、list集合、数组等时,cxf可以处理

3.       还有一些像map、非javabean式的复合类,cxf是处理不了的

 

Web service的三个技术基础:

1.       wsdl  web service definition language ——web service定义语言

2.       soap

3.       uddl

一次web service的调用——其实并不是方法调用,而是发送soap消息(即xml文档片段)

 

 

 

 

 

 

 

阅读技巧从下往上阅读

 

 

调用一次web service的本质:

1.       客户端把调用方法参数,转换xml文档片段——该文档片段必须符合wsdl定义的格式

2.       通过网络,把xml文档片段从传给服务器

3.       服务器接受到xml文档片段

4.       服务器解析xml文档片段,提取其中的数据。并把数据转换调用webservice所需要的参数值

5.       服务器执行方法

6.       把执行方法得到的返回值,再次转换生成为xml文档片段(soap消息)——该文档片段必须符合wsdl定义的格式

7.       通过网络、把xml文档片段传给客户端

8.       客户端接收到xml文档片段

9.       客户端解析xml文档片段,提取其中的数据。并把数据转换调用webservice所需要的返回值

 

从上面调用本质来看,要一个语言支持web service

唯一的要求是:该预言支持xml文档解析、生成、支持网络传输。

 

 

 

cxf开发中,如果遇到cxf无法处理的类型,就需要程序员自行处理

(1)       使用@XmlJavaTypeAdapter注解修饰

使用annotation时,通过value属性指定一个转换器

2)实现自己的转换器。

 

 

 

 

 

Web service 如何进行权限控制?

解决思路:服务器端要求input消息总是携带有用户名密码信息

如果没有直接拒绝调用

 

Cxf引入拦截器

服务器端添加拦截器

 

 

 

 

 

 

wsdl2java –encoding utf-8 http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

 

 

注释注解:@WSDLDocumentation(value="注释")

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Spring cxf 整合

1.spring发布SOAP方式web服务(jax-ws

 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>ch03_1</display-name>

 

   <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/beans-server.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>/ws/*</url-pattern>

  </servlet-mapping>

 

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>

 

beans-server.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"

    xmlns:jaxrs="http://cxf.apache.org/jaxrs"

    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

    http://cxf.apache.org/jaxrs

    http://cxf.apache.org/schemas/jaxrs.xsd">

   

    <!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 -->

    <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" />

 

    <!-- CXF 提供的内置拦截器 -->

    <bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />

    <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />

   

 

    <!-- 配置方案一  使用JAX-WS标准配置jaxws:endpoint,发布服务 -->

    <!-- id:spring bean标识,implementor:服务实现类,address:服务发布路径 -->

    <jaxws:endpoint id="merchant_1"

            implementor="com.cxfdemo.server.service.impl.IMerchantServiceImpl" address="/m_1">

        <!-- 可选配置入口拦截器 -->

        <jaxws:inInterceptors>

            <ref bean="inLoggingInterceptor" />

        </jaxws:inInterceptors>

        <!-- 可选配置出口拦截器 -->

        <jaxws:outInterceptors>

            <ref bean="outLoggingInterceptor" />

        </jaxws:outInterceptors>

    </jaxws:endpoint>

   

    <!-- 配置方案二  使用JAX-WS标准配置jaxws:endpoint,发布服务 -->

    <!-- id:spring bean标识,serviceClass:服务实现接口,address:服务发布路径 -->

    <jaxws:server id="merchant_2" serviceClass="com.cxfdemo.server.service.IMerchantService" address="/m_2">

        <!-- 注入:服务实现类 -->

        <jaxws:serviceBean>

            <ref bean="merchantService"/>

        </jaxws:serviceBean>   

       

        <!-- 可选配置入口拦截器 -->

        <jaxws:inInterceptors>

            <ref bean="inLoggingInterceptor" />

        </jaxws:inInterceptors>

        <!-- 可选配置出口拦截器 -->

        <jaxws:outInterceptors>

            <ref bean="outLoggingInterceptor" />

        </jaxws:outInterceptors>

       

    </jaxws:server>

    <!-- 服务实现类 -->

    <bean id="merchantService" class="com.cxfdemo.server.service.impl.IMerchantServiceImpl" />

 

</beans>

 

 

<?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"

    xmlns:jaxrs="http://cxf.apache.org/jaxrs"

    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

    http://cxf.apache.org/jaxrs

    http://cxf.apache.org/schemas/jaxrs.xsd">

   

    <!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 -->

    <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" />

   

    <!-- spring管理ws client -->

    <!-- address:ws服务URL  serverClass:客户端服务接口类(非实现类)交由Spring生成客户端代理对象 -->

    <jaxws:client id="merchantServiceClient" address="http://localhost:8088/ch03_1/ws/m_2"

                                                        serviceClass="com.cxfdemo.server.client.IMerchantService"/>

   

 

</beans>

 

 

 

 

2.Rest

 

<?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"

    xmlns:jaxrs="http://cxf.apache.org/jaxrs"

    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

    http://cxf.apache.org/jaxrs

    http://cxf.apache.org/schemas/jaxrs.xsd">

   

    <!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 -->

    <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" />

 

    <!-- CXF 提供的内置拦截器 -->

    <bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />

    <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />

   

    <jaxrs:server id="merchantrs" address="/">

       <jaxrs:serviceBeans>

           <ref bean="merchantService"/>

       </jaxrs:serviceBeans>

      

       <jaxrs:extensionMappings>

            <entry key="json" value="application/json" />

            <entry key="xml" value="application/xml" />

        </jaxrs:extensionMappings>

       

       <!-- 可选配置入口拦截器 -->

       <jaxrs:inInterceptors>

           <ref bean="inLoggingInterceptor" />

       </jaxrs:inInterceptors>

       <!-- 可选配置出口拦截器 -->

       <jaxrs:outInterceptors>

           <ref bean="outLoggingInterceptor" />

       </jaxrs:outInterceptors>   

    </jaxrs:server>

 

    <!-- 服务实现类 -->

    <bean id="merchantService" class="com.cxfdemo.server.service.impl.IMerchantServiceImpl" />

 

</beans>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

软件技术开发分类:

1.本地调用(LPClocal procedure call

 

2.远程调用(RPCremote procedure call

2.1 远程方法调用(RMIremote method invocation

2.2 通用对象请求代理体系结构(CORBAcommon object request broker architecture

    IDL:接口描述语言 interface descripton language

2.3 互联网内部对象请求代理协议(RMI-IIOPinternet inner-object protocat

2.4 组件对象模型(COMCOM+component object model

2.5 web service

协议:

http超文本传输协议

SOAP(简单对象访问协议) simple object access protocol

 

Soa  service oriented architecture

 

ESB(企业服务总线) entertainment service bus

 

Sca service component architechture

 

 

 

 

 

 

 

  • 大小: 12.5 KB
  • 大小: 16.5 KB
  • 大小: 7.3 KB
  • 大小: 24 KB
  • 大小: 27.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics