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