使用CXF開發RestFul風格WebService

1.首先定義一個簡單類DD

package service;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="DD")
@XmlAccessorType(XmlAccessType.FIELD)
public class DD {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

這個類主要用於封裝返回的數據。
@XmlRootElement(name="DD")
    當返回類型是xml方式的時候,指定根節點的名稱爲DD
@XmlAccessorType(XmlAccessType.FIELD)
    表示將該類中的屬性字段(非靜態)作爲序列化的屬性或元素
2.定義Service類,對外提供獲取數據的方法
package service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Component;

@Path("/directoryservice")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) 
@Component("directoryService")
public class DirectoryService {
	@GET
	@Path("/directory/{id}")
	public DD getDirectory(@PathParam("id") String id) {
		DD dd = new DD();
		dd.setId(id);
		return dd;
	}
}

@Path("/directoryservice")
    該Service部署的路徑
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) 
    支持的返回數據的類型
@Component("directoryService")
    註解,讓spring掃描並實例化該類,directoryService爲spring實例化的bean的id
@Path("/directory/{id}")
    該方法的發佈路徑,{id}表示參數
@PathParam("id")
    指定參數對應的路徑中的參數名稱,這裏指定將路徑中的{id}參數,注入到該方法中的id參數
3.spring配置
<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/context
           			   http://www.springframework.org/schema/context/spring-context-3.2.xsd
                       http://www.springframework.org/schema/tx 
           			   http://www.springframework.org/schema/tx/spring-tx-3.2.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">
	<!-- cxf配置 -->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
		
	<!-- restful風格服務配置 -->
	<jaxrs:server id="restServiceContainer" address="/">
		<jaxrs:serviceBeans>
			<ref bean="directoryService" />
		</jaxrs:serviceBeans>
		<!--返回json格式數據必須配置 -->
		<jaxrs:providers>  
            <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />  
        </jaxrs:providers> 
		<jaxrs:extensionMappings>
			<entry key="json" value="application/json" />
			<entry key="xml" value="application/xml" />
		</jaxrs:extensionMappings>
	</jaxrs:server>
	
	
	<!-- 自動掃描組件 -->
	<context:component-scan base-package="service" />

	<bean id="webServicesAgent" class="org.apache.cxf.spring.remoting.Jsr181HandlerMapping">
		<property name="urlPrefix">
			<value>/</value>
		</property>
	</bean>
</beans>
4.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" 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>restful</display-name>
  <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>
  <!--指定spring配置文件位置-->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:beans.xml</param-value>
  </context-param>
  <!-- 配置spring的監聽器 -->
	<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>
</web-app>

5.訪問效果


2.7版本代碼:點擊打開鏈接
3.1.4版本的cxf實現:點擊打開鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章