使用CXF开发RestFul风格WebService
2015-12-09 11:56
633 查看
1.首先定义一个简单类DD
这个类主要用于封装返回的数据。
@XmlRootElement(name="DD")
当返回类型是xml方式的时候,指定根节点的名称为DD
@XmlAccessorType(XmlAccessType.FIELD)
表示将该类中的属性字段(非静态)作为序列化的属性或元素
2.定义Service类,对外提供获取数据的方法
@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配置
5.访问效果
http://localhost:8080/restful/directoryservice/directory/1
指定返回数据的格式
http://localhost:8080/restful/directoryservice/directory/1?_type=json
2.7版本代码:点击打开链接
3.1.4版本的cxf实现:点击打开链接
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.访问效果
http://localhost:8080/restful/directoryservice/directory/1
指定返回数据的格式
http://localhost:8080/restful/directoryservice/directory/1?_type=json
2.7版本代码:点击打开链接
3.1.4版本的cxf实现:点击打开链接
相关文章推荐
- Android File类 根据官方文档理解
- Java Web 前端高性能优化(二)
- 详解JavaScript基本类型和引用类型
- PHP require和include 路径问题
- 几何公式总结
- MySQL中describe命令的使用方法小结
- 第8章总结
- C#中if和#if区别
- window疑难问题解决
- VB 半透明窗体
- 请谨慎调用ShowWindow
- Android5.0 AsyncTask的实现分析
- Volley源码分析 (三):如何分发结果
- 快速部署tomcat项目的Shell脚本
- requireJS教程
- 汇编命令
- Nginx ngx_http_gunzip_module
- 算法基础(1)
- iOS 保持界面流畅的技巧(转)
- [javase学习笔记]-1.1 基本常识