webServer-----Spring 集成cxf笔录

目前webserver主要有俩中方式:1,传统的webserver标准集成方式-生成WSDL的xml文档.       2, 基于restful风格的webserver

 

 

java RESTful WebServer

一、概述

JAX-RS是Java提供用于开发RESTful Web服务基于注解(annotation)的API。JAX-RS旨在定义一个统一的规范,使得Java程序员可以使用一套固定的接口来开发REST应用,避免了依赖第三方框架。同时JAX-RS使用POJO编程模型和基于注解的配置并集成JAXB,可以有效缩短REST应用的开发周期。JAX-RS只定义RESTful API,具体实现由第三方提供,如Jersey、Apache CXF等。

JAX-RS包含近五十多个接口、注解和抽象类:

javax.ws.rs包含用于创建RESTful服务资源的高层次(High-level)接口和注解。

javax.ws.rs.core包含用于创建RESTful服务资源的低层次(Low-level)接口和注解。

javax.ws.rs.ext包含用于扩展JAX-RS API支持类型的APIs。

JAX-RS常用注解:

@Path:标注资源类或方法的相对路径。

@GET、@PUT、@POST、@DELETE:标注方法的HTTP请求类型。

@Produces:标注返回的MIME媒体类型。

@Consumes:标注可接受请求的MIME媒体类型。

@PathParam、@QueryParam、@HeaderParam、@CookieParam、@MatrixParam、@FormParam:标注方法的参数来自于HTTP请求的位置。@PathParam来自于URL的路径,@QueryParam来自于URL的查询参数,@HeaderParam来自于HTTP请求的头信息,@CookieParam来自于HTTP请求的Cookie。

 

 

spring集成cxf详解:

本次集成适用版本是cxf3.X以上版本spring3.2

<!-- CXF需要导入的jar包 --> 

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.0.3</version>
</dependency>
 <!-- CXF实现RestFul接口需要用到的包 -->  
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxrs</artifactId>
    <version>3.0.3</version>
</dependency>

 <!-- 客户端调用restFul服务需要导入的包 -->  

        <dependency>  

           <groupId>org.apache.cxf</groupId>  

            <artifactId>cxf-rt-rs-client</artifactId>  

            <version>${cxf.version}</version>  

        </dependency>  

在以前2.x的CXF上,bug和配置上很复杂。3.0以后很方便,

 web.xml

 <!-- CXF -->  

<servlet>  

<servlet-name>cxf</servlet-name>  

<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  

<load-on-startup>1</load-on-startup>  

</servlet>  

<servlet-mapping>  

<servlet-name>cxf</servlet-name>  

<url-pattern>/services/*</url-pattern>  

   </servlet-mapping>

 

 

 

 

spring.xml配置

 

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

发布webserver 普通接口

<!-- 接口实例方式webservice接口发布 -->
<jaxws:endpoint id="WebserviceImpl" implementor="com.slwy.ticket.testcase.impl.WebserviceImpl" address="/getUser"/>

<!-- 传统方式webservice接口 -->
<bean id="departServerImpl" class="com.slwy.ticket.server.systemsetServer.impl.DepartServerImpl"></bean>
<jaxws:server id="departservice" serviceClass="com.slwy.ticket.server.systemsetServer.DepartServer" address="/departments">
<jaxws:serviceBean>
<ref bean="departServerImpl"/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>

</jaxws:server>
 

发布webserver RestFul接口

<!-- address-请求路径 -->
    <jaxrs:server id="imaoPayService" address="/ipservice">
        <!-- 输入拦截器设置 -->
        <jaxrs:inInterceptors>
        </jaxrs:inInterceptors>
         
        <!-- 输出拦截器设置 -->
        <jaxrs:outInterceptors>
        </jaxrs:outInterceptors>
          
        <!-- serviceBeans-暴露的WebService服务类 -->
        <jaxrs:serviceBeans
            <ref bean="regUser" />
        </jaxrs:serviceBeans>
           
        <!-- 支持的协议 -->
        <jaxrs:extensionMappings
            <entry key="json" value="application/json" /> 
            <entry key="xml"  value="application/xml" /> 
        </jaxrs:extensionMappings
          <!-- json支持格式 -->
        <jaxrs:providers>
          <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
        </jaxrs:providers>
        <!-- 编码格式 -->
        <jaxrs:languageMappings>
               <entry key="en" value="en-gb"/>
        </jaxrs:languageMappings>         
    </jaxrs:server>
 

  总结如下:

    @POST 表示HTTP的访问模式

    @Path 表示访问路径

    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 响应内容 MIME 类型

 jaxws:endpoint(cxf风格)是定义的一个soap WebService,另一种jaxws:server标签(xfire风格,因为Apache CXF = Celtix + XFire ,是XFire的演进版)可以用。

 

 我通过设置jsonProvider使其能够在response的时候返回一个json格式的数据:

 

 
 
 
 
 
 
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">
</bean>
 
<jaxrs:providers>
   <ref bean="jsonProvider"/>                       
</jaxrs:providers>

 

 

附:构建项目的完整依赖包:maven

 

 

<!--cxf框架开始-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.0.3</version>
</dependency>

<dependency>
<groupId>org.apache.ws.xmlschema</groupId>
<artifactId>xmlschema-core</artifactId>
<version>2.1.0</version>
</dependency>

<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>

<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>

<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>3.0.3</version>
</dependency>

<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.3</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>3.0.3</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-wsdl</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-jaxb</artifactId>
<version>3.0.3</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.3</version>
</dependency>
<!--spring cxf支持resfull-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-simple</artifactId>
<version>3.0.3</version>
</dependency>

<!--cxf结束-->

<!--json数据包-->
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
</dependency>

<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>

<!--restfull-->
<!--<dependency>-->
<!--<groupId>javax.ws.rs</groupId>-->
<!--<artifactId>javax.ws.rs-api</artifactId>-->
<!--<version>2.0.1</version>-->
<!--</dependency>-->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<!--json数据包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-annotations.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.7.0</version>
</dependency>

 

posted @ 2016-11-11 15:40  markchuan  阅读(310)  评论(0编辑  收藏  举报