当前位置: 代码迷 >> Web前端 >> Apache CXF REST WebService容易应用
  详细解决方案

Apache CXF REST WebService容易应用

热度:838   发布时间:2012-10-06 17:34:01.0
Apache CXF REST WebService简单应用

<p>????? 本文目的就项目中的Apache CXF中的REST WebService风格的展现。下面言归正传,看实惠,代码如下:

?

?

</p>

package com.easyway.rest.webservice;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.ext.MessageContext;
/**
 * Apache CXF REST方式发布WebService的总结
 * 1.定义Apache CXF CXFServlet的服务发布拦截类
 *    org.apache.cxf.transport.servlet.CXFServlet;
 * 2.定义相关REST WebService的服务类
 *      RestWebService
 * 3.在相关的Spring的配置文件配置相关的参数
 *   <pre>
 *     <import resource="classpath:META-INF/cxf/cxf-all.xml"/>
 *              
*		<http-conf:conduit name="*.http-conduit">
*			<http-conf:client ReceiveTimeout="600000" ConnectionTimeout="600000"/><!-- 10分钟 -->
*		</http-conf:conduit>
*
*
*        <bean id="RestWSBean" class="com.easyway.rest.webservice.RestWebService"/>
*        
*        <jaxrs:server id="RestWS" address="/restWS">
*			<jaxrs:serviceBeans>
*				<ref bean="restService" />
*			</jaxrs:serviceBeans>
*		</jaxrs:server>
* </pre>
 * 4.发布相关的Web服务
 *   查看相关的服务:
 *    例如:
 *       http://localhost:8080/ApacheCXFREST/service/
 *        ApacheCXFREST为Web项目上下文
 *        service为Apache CXF在Web.xml容器中配置的拦截路径
 *   查看WADL的路径:
 *      http://localhost:8080/ApacheCXFREST/service/restWS?_wadl&_type=xml
 *        ApacheCXFREST为Web项目上下文
 *        service为Apache CXF在Web.xml容器中配置的拦截路径
 *        restWS为Spring的中配置的REST服务的拦截的路径
 *        _wadl&_type=xml为WADL的协议的格式
 *        
 *         <resources base="http://localhost:8080/ApacheCXFREST/service/restWS">
 * 			<resource path="/rest">
 * 			<method name="GET">
 * 			..............
 *   请求的路径=resources元的base属性+resource元素的path属性
 *      例如:http://localhost:8080/ApacheCXFREST/service/restWS/rest
 * 5.请求相关的服务
 *   例如
 *
 * @author longgangbai
 *
 */
@Path("/rest")
public class RestWebService {
	
	private MessageContext messageContext;
	
	
	/**
	 * 存储发送的数据
	 * @param messageContext
	 */
	@Context
	public void setMessageContext(MessageContext messageContext) {
		this.messageContext = messageContext;
	}
	/**
	 * 
	 * 领导慰问士兵们的
	 * 
	 * @param username xx
	 * @return
	 */
	@GET()
	@Consumes(MediaType.TEXT_PLAIN)
	public String helloworld(){
		return "xx说: 同志们辛苦了 !!   ";
	}
	/**
	 *   当需要Servlet环境采用注释中的内容获取客户端中的Servlet环境的中的各种信息
	 *   如上传,下载之类,可能需要知道比较多的信息
	 *   采用这种方式
	 *   
	@SuppressWarnings("unchecked")
	@POST
	@Produces(MediaType.TEXT_PLAIN)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String postMethod(MultivaluedMap<String, Object> formParams){
		Map<String,Object> requestMap=null;
		boolean isGet=false;
		if(formParams!=null){
			requestMap=(Map)formParams;
		}else{
			try {
				this.messageContext.getHttpServletRequest().setCharacterEncoding("UTF-8");
				this.messageContext.getHttpServletResponse().setCharacterEncoding("UTF-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			requestMap=this.messageContext.getHttpServletRequest().getParameterMap();
			isGet=true;
		}
		
		return "";
	}
	*/
	
	
	/**
	 *广大士兵 回答
	 * @return
	 */
	@POST()
	@Produces(MediaType.TEXT_PLAIN)
	public String service(){
		return "士兵说:为人民服务";
	}
}

?

?

spring关于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:jaxrs="http://cxf.apache.org/jaxrs"
                 xmlns:jaxws="http://cxf.apache.org/jaxws"
                 xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
                 xsi:schemaLocation="
                       http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://cxf.apache.org/jaxrs
                       http://cxf.apache.org/schemas/jaxrs.xsd
                       http://cxf.apache.org/jaxws
                       http://cxf.apache.org/schemas/jaxws.xsd
                       http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">
 
       <!-- 针对CXF发布服务的需要的配置的加载的导入 -->
        <import resource="classpath:META-INF/cxf/cxf-all.xml"/>
        <!-- 设置服务相关的请求的配置 -->        
		<http-conf:conduit name="*.http-conduit">
			<http-conf:client ReceiveTimeout="600000" ConnectionTimeout="600000"/><!-- 10分钟 -->
		</http-conf:conduit>

        <!-- 
          	设置需要发布的bean
         -->
        <bean id="RestWSBean" class="com.easyway.rest.webservice.RestWebService"/>
        <!-- 
          	发布相关的WebService
         -->
        <jaxrs:server id="RestWS" address="/restWS">
			<jaxrs:serviceBeans>
				<ref bean="RestWSBean" />
			</jaxrs:serviceBeans>
		</jaxrs:server>

		
</beans>

?

?

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">
   <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <display-name>RESTWebServices</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContent-*-services.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>/service/*</url-pattern>
  </servlet-mapping>
</web-app>

?

  相关解决方案