【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)

转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.htmlphp

 

一,选择一个合适的,Web开发环境:html

   我选择的是Eclipse for J2EE,固然你们能够选择MyEclipse我只是嫌最新版的MyEclipse Crack太烦,因此没用它。当年我也是最喜欢它的哟。若是你手头只有Eclipse for Java不要紧,安装一个WTP就能够了。java

   a.首先建立一个Dynamic Web Project :linux

 

     在建立的第一页,Project Name 我用的是"MyServices"中要在Target Runtime中选择一个容器服务器,我这里测试环境下,选择的是Tomcat ,若是没有,能够在右边点击“New Runtime"进行新增Tomcat 运行环境。web

     在Project建立的最后一页最好选中建立一个web.xml免得你去建立,缺省状况下,这个web.xml只配置了欢迎页面。如index.php等。spring

  b.测试一下,是否这个环境能够发布Build后的代码及网页到Tomcat测试环境中运行:apache

     在左边的"Web Content"目录下的”WEB-INF"子目录,建立一个index.jsp文件进行测试。建立完成后,从新build项目,而后在右下方的Server Tab里会自动列出你刚才选择的Tomcat发布环境。你点击这个Tomcat Serverjson

    个人环境是Tomcat Server 7.0 而后右键菜单中选择“Publish" 你会看到 服务器,及服务器下面你的项目右边会由”Republish"变成“Synchronized" ,说明工程编译结果都已成功发布到Tomcat Server测试环境下了。浏览器

    点击Tomcat Server 7.0 选择”Start" 服务启动,你就能够在你的网页里输入“http://localhost:8080/MyServices/index.jsp"你就能够看到你的网页内容了。tomcat

   由于这个网站插图不方便,有时间再插一些图示吧。

 

二,关于cxf 框架的运行时序的我的思考过程:

     0.Tomcat启动时,加载web.xml,根据web.xml的配置,把CXF 的Servlet 放到Tomcat的侦听端口的映射中,同时也把Spring的Bean加载器也加载到Tomcat的进程空间中,Spring根据初始化的配置文件(好比application-context.xml),加载Bean对象。在加载过程当中,会根据配置文件中的xml中的xmlns进行分析(好比,...xmlns:jaxrs ="http://cxf.apache.com", 先到

spring的handlers配置文件中,根据“http://cxf.apache.com"字符串找到对映的handler组件,用这个组件加载全部配置文件中,以jawrs为xml命名空间的配置部分,好比,<jaxrs:server id="services" address="/">     <jaxrs:serviceBeans>       <bean class="com.services.rest.HelloWorld" />     </jaxrs:serviceBeans>     <jaxrs:providers>  可参考:http://blog.csdn.net/javabenface/article/details/7441923)调用对应的加载器进行解释加载,这里调用cxf的加载器进行加载。cxf加载器会根据 beans.xml中对应的项加载最终实现的class文件,这些class在对应的java源文件编译过程当中,根据java文件中的annomation标记符进行处理,使得这些class加载时造成正确的path 与对象的映射。

     1.客户端发出请求,经过XML/HTTP把请求及参数对象转为XML通过HTTP传到Servlet容器中。

     2.CXF会根据Path与对象的映射找到正确的对象,若是是Restful Web Service还会再根据映射找到Path中对应的执行方法。

三,建立一个基于CXF及Spring的SOAP Web Service:

     1.建立Web Service 相关的类:

       由于这种类型Web Service是SOA(面象服务架构)的,因此是提供一个远程的RPC接口。因此首先要有一个接口,固然,在服务端要提供真正的服务,因此要有一个这个接口在服务端的实现。下面分别实现:

 IHelloWorld.java:  

package com.services.soap;

import javax.jws.WebParam;

import javax.jws.WebService;

@WebService 

public interface IHelloWorld {

        public String speakoutUserInfo(@WebParam(name = "param") ParamDTO obj);

}

HelloWorld.java:

package com.services.soap;

import javax.jws.WebService;

/**  *   * 使用@WebService指向Interface定义类便可.  */ 

@WebService(endpointInterface = "com.services.soap.IHelloWorld") 

public class HelloWorld implements IHelloWorld{

 @Override

 public String speakoutUserInfo(ParamDTO obj) {

  // TODO Auto-generated method stub

        return "hello";  

}

}

上述的服务实现用到一个对象,这个对象能够作为参数远程进行传递,通常叫作DTO(数据传输对象)。固然你能够不用对象,用普通的数据类型这个实例一次性都表现一下。

ParamDTO.java:

package com.services.soap;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlType;

/**  

* Web Service传输信息的DTO.  

* 分离entity类与web service接口间的耦合,隔绝entity类的修改对接口的影响. 使用JAXB 2.0的annotation标注JAVA-XML映射,尽可能使用默认约定.  *   

*/ 

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "User") 

public class ParamDTO  {

        protected Integer id;

        protected String name; 

         public Integer getId()      {

              return id;

        }

        public void setId(Integer value)      {

              id = value;

       }

        public String getName()      {

              return name;

      }

        public void setName(String value)      {

             name = value;

      } 

}

2.在配置文件中体映射这个Service:

咱们定义这个Beans.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">    
    <jaxws:endpoint id="webServiceHelloWorld"  
        address="/HelloWorld" implementor="com.services.soap.HelloWorld"/>  
</beans> 

这个Beans.xml放到Spring的加载Beans的配置文件中被引用:

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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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" />  
    <import resource="classpath:beans.xml" />  //注意这行代码的引用
    
</beans> 

固然咱们要在Web.xml中配置Spring:

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>webrest</display-name>
  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>WEB-INF/classes/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>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <url-pattern>/*</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>

也可参考http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html提供了另外一种类似的配置方式。

四,建立一个基于CXF及Spring的Restful Web Service:

  这个就相对简单了。由于经不须要直接接供RPC接口给客户端,只是其于ROA的方式提供资源的操做,能够理解为基于一些xml,json的表达一些资源对象变态变化的传输同步给远程服务。

  因此经过xml映射对象,Annomation进行直接映射方法与path.因此直接写实现类就好了,固然cxf还有别的框架有其它的映射或配置方式。

 a.代码实现:

  先上代码:

 HelloWorld.java

package com.services.rest;

 import javax.ws.rs.Consumes;

 import javax.ws.rs.GET;

 import javax.ws.rs.POST;

 import javax.ws.rs.Path;

 import javax.ws.rs.PathParam;

 import javax.ws.rs.Produces;

 import javax.ws.rs.core.Response;

 import org.codehaus.jackson.jaxrs.JacksonJsonProvider;  

 @Path("/hello")

 public class HelloWorld {

     @GET

     @Path("/echo/{input}")

     @Produces("text/plain")

     public String ping(@PathParam("input") String input) {

         return input + ":in server!";

     }

     @POST

     @Produces("application/json")

     @Consumes("application/json")

     @Path("/jsonBean")

     public Response modifyJson(JsonBean input) {

            input.setCommand(222);

            input.getParam().put("content", "welcome to server!");

           return Response.ok().entity(input).build();

     }

}

其中用到JsonBean对象这个是能够远程传送参数对象,通常状况无需特别的定义。就能够直接用了。我这里定义以下:

JsonBean:

package com.services.rest;

import java.util.Map;

public class JsonBean {

 private Integer command;

 private Integer protocolVersion;

 private String  platformType;

 private Map<String, Object> param;

 public Integer getCommand() {

    return command;

 }

 public void setCommand(Integer command) {

     this.command = command;

 }

 public Integer getProtocolVersion() {

     return protocolVersion;

 }

 public void setProtocolVersion(Integer protocolVersion) {

     this.protocolVersion = protocolVersion;

 }

 public String getPlatformType() {

     return platformType;

 }

 public void setPlatformType(String platformType) {

    this.platformType = platformType;

 }

 public Map<String, Object> getParam() {

     return param;

 }

 public void setParam(Map<String, Object> param) {

     this.param = param;

 }

 }

 b.进行发布:

配置一个rest-services.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:jaxrs="http://cxf.apache.org/jaxrs"   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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

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

  <context:property-placeholder/>   <context:annotation-config/>   <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"/>   <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"/>

   <jaxrs:server id="services" address="/">     <jaxrs:serviceBeans>       <bean class="com.services.rest.HelloWorld" />     </jaxrs:serviceBeans>     <jaxrs:providers>         <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>     </jaxrs:providers>     </jaxrs:server>

</beans>

在Spring的加载配置文件(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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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" />  
    <import resource="classpath:rest-services.xml" /> 
</beans>

OK,大功告成。

到时此咱们能把这两种模式的Web Service同时在一个框架里发布吗?固然能够:)要作的只有一步,就是在上面的applicationContext.xml里同时加载两个Service的映射文件就能够了。

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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" /> 

    <import resource="classpath:beans.xml" /> 
    <import resource="classpath:rest-services.xml" /> 
</beans>

如今就你就能够编译完成,Publish到你的tomcat上进行测试了,不过必定要注意,在发布选项里必定要把你项目工程中引用的jar依赖库(好比,cxf相关,spring相关的,Json相关的)同时发布到你的Tomcat Server的运行环境里,这里只须要修改:项目(MyServices)右键=》Properties=>Deployment Assembly=>Add=>Java Build Path Entries 不过在引入的jar过多时可能会形成冲突,假如在测试时,说CXF 的一个Discoveryxxx对象..... Null Point之类的错误:

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookservice': Invocation of init method failed; nested exception is org.apache.cxf.service.factory.ServiceConstructionException
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1422)
        .......

        Caused by: org.apache.cxf.service.factory.ServiceConstructionException
        at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:201)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              ...

 Caused by: java.lang.NullPointerException
        at org.apache.cxf.ws.discovery.listeners.WSDiscoveryServerListener.startServer(WSDiscoveryServerListener.java:64)
        at org.apache.cxf.bus.managers.ServerLifeCycleManagerImpl.startServer(ServerLifeCycleManagerImpl.java:61)
        at org.apache.cxf.endpoint.ServerImpl.start(ServerImpl.java:146)
        at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:192)
          ........

就是最多见的cxf-services-ws-discovery-service-2.x.x.jar冲突,去掉这个.jar的依赖便可。若是你在项目的Java Build Path中去掉这个jar仍不行,就去你测试的Tocat Server上右键“clean" 而后再"Publish",若是这样还不行,说明是Eclipse 清除Tomcat的发布目录不完全(Eclipse也有不少bug的),你就去Tomcat 的运行时临时Web根目录中去清除这个jar.这个目录是在Eclipse的Workspace目录下的”.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps“子目录。如今估计你能理解为何你在Eclipse 的runtime server中用Tomcat测试发布后的文件在Tomcat的安装目录看不到的缘由了吧?呵呵,由于Eclipse整合tomcat测试运行时,根本上会使用本身的临时目录做为Tomcat的运行时Web根目录。

若是你遇到:

Caused by: java.lang.NullPointerException

Class Could not found: org.springframework.web.context.ContextLoaderListener 之类的错误。你须要在你的Web project的Deployment Assemblly 中 加入Java build path的库,即点击"Add"按钮,在弹出列表窗口中选择“Java Build Path Entries"而后选中你的工程发布所须要的库便可。

到这里应该完成了。

下面就能够用各类客户端或者浏览器进行访问了,这里主要讲方法,可能部分代码在相关的博文里面附上了:

0.测试工具:

   对于restful web service由于返回的内容均可以简单的分析,因此能够用不少工具进行测试。

a. 基于firefox的 Poster

b.linux上的curl.

c.......

1.Native 客户端访问方法:

  用Java的NIO中的HttpClient就能够搞定 Restful Web Service.

a.cxf的WebClient接口:

 cxf提供了访问WebService的全部接口,例子代码以下:

import javax.ws.rs.core.MediaType;
import org.apache.cxf.jaxrs.client.WebClient;
public class RSETClient {

    private static WebClient client;

    public void init() {
         client = new WebClient("http://localhost:8080/restWeb/hello/teststring");

    }

    public void testGet() {

        System.out.println(client.path("sample").accept(MediaType.TEXT_PLAIN).get(String.class));
  }
}

b.Spring RestTemplate:

这个能够经过在客户端使用Spring 的RestTemplate 相关的库来访问。

 下面代码是用Spring for Android写的,PC各平台上调用大同小异,没时间在这里上代码了。

 HttpHeaders reqHeader = new HttpHeaders();
  reqHeader.setContentType(new MediaType("text", "plain"));
  HttpEntity<String> req = new HttpEntity<String>(reqHeader);
  String restUrl = "http://192.168.2.100:8080/webrest/hello/echo/testtest";// 这个地址要根据你Restful
                     // 服务器来定
  // 好戏上场了,呵呵
  RestTemplate restTemplate = new RestTemplate(true);
  
  //restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
  ResponseEntity<String> response = restTemplate.exchange(restUrl,
    HttpMethod.GET, req, String.class);
  String msgBody = response.getBody();

 System.out.println(msgBody);

2.浏览器测试:

a.能够经过Form进行简单的访问。

b. 能够经过客户端的Ajax代码来访问Restful Webservice.

能够经过客户端的Ajax代码来访问Restful Webservice.

3.手机端访问方法(访问代码见个人相关手机客户端的博文):

a.Android端:

 可使用Android自带的HttpClient进行访问Restful Web Service。这就是Restful Web Service的优点能够一些平台上最基本的http 库来访问。

 可使用第三方库KSoap来访问基于Soap的Web Service.

 同时你能够用Spring for Android 中的RestTemplate接口来访问 Restful Web service :

HttpHeaders reqHeader = new HttpHeaders();

  reqHeader.setContentType(new MediaType("text", "plain"));

  HttpEntity<String> req = new HttpEntity<String>(reqHeader);

  String restUrl = "http://192.168.2.100:8080/webrest/hello/echo/testtest";// 这个地址要根据你Restful

   // 服务器来定   // 好戏上场了,呵呵

  RestTemplate restTemplate = new RestTemplate(true);

    //restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

  ResponseEntity<String> response = restTemplate.exchange(restUrl,     HttpMethod.GET, req, String.class);

  String msgBody = response.getBody();

  System.out.println(msgBody);

 

b.IOS端:

可使用IOS上的第三方Http库来访问 Restful Web Service,库名字叫:。

相关文章
相关标签/搜索