使用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实现: 点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值