使用CXF与Spring集成实现RESTFul WebService
例如,GET http://host/context/employees/12345 将获取 ID 为 12345 的员工的表示。这个响应表示可以是包含详细的员工信息的 XML 或 ATOM,或者是具有更好 UI 的 JSP/HTML 页面。您看到哪种表示方式取决于服务器端实现和您的客户端请求的 MIME 类型。
RESTful Web Service 是一个使用 HTTP 和 REST 原理实现的 Web Service。通常,一个 RESTful Web Service 将定义基本资源 URI、它所支持的表示/响应 MIME,以及它所支持的操作。
<properties>
<spring.version>4.0.5.RELEASE</spring.version>
<jackson.version>1.9.2</jackson.version>
<cxf.version>3.0.3</cxf.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <!--spring核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> <!--JSTL标签类-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<!--cxf-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency> <!--json-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
配置application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd "> <!--扫描器-->
<context:component-scan base-package="com.wangzhu"/> </beans>
配置applicationWebService.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <!--
<import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>-->
<jaxrs:server id="webService" address="/">
<!--输入拦截器设置-->
<jaxrs:inInterceptors> </jaxrs:inInterceptors> <!--输出拦截器设置-->
<jaxrs:outInterceptors> </jaxrs:outInterceptors> <!--serviceBeans:暴露的WebService服务类-->
<jaxrs:serviceBeans>
<ref bean="studentService"/>
</jaxrs:serviceBeans> <!--支持的协议-->
<jaxrs:extensionMappings>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
</jaxrs:extensionMappings>
<!--编码格式-->
<jaxrs:languageMappings> </jaxrs:languageMappings>
<!--对象转换-->
<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider"/>
</jaxrs:providers> </jaxrs:server>
</beans>
备注:注意以上文件中的标记的地方!其中address可以用于指定Web Service的版本!
配置web.xml
<!--全局配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param> <!--spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--cxf的Servlet-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
实体类StudentVo .java
package com.wangzhu.vo; import java.io.Serializable; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement
public class StudentVo implements Serializable { /**
*
*/
private static final long serialVersionUID = -5481609827444603990L;
private Integer id;
private String name;
private Integer age;
private String desc;
private Boolean status; public StudentVo() {
} public StudentVo(Integer id, String name, Integer age, String desc,
Boolean status) {
this.id = id;
this.name = name;
this.age = age;
this.desc = desc;
this.status = status;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} public Boolean getStatus() {
return status;
} public void setStatus(Boolean status) {
this.status = status;
} @Override
public String toString() {
return "{id=" + id + ", name=" + name + ", age=" + age + ", desc="
+ desc + ", status=" + status + "}";
} }
实体类StudentListVo.java
package com.wangzhu.vo; import java.io.Serializable;
import java.util.List; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement
public class StudentListVo implements Serializable { /**
*
*/
private static final long serialVersionUID = -2200047544608641662L;
private List<StudentVo> studentList; public StudentListVo() {
} public StudentListVo(List<StudentVo> studentList) {
this.studentList = studentList;
} public List<StudentVo> getStudentList() {
return studentList;
} public void setStudentList(List<StudentVo> studentList) {
this.studentList = studentList;
} @Override
public String toString() {
return "{studentList=" + studentList + "}";
} }
数据生成器StudentDateSet.java
package com.wangzhu.dataset; import java.util.ArrayList;
import java.util.List; import org.springframework.stereotype.Component; import com.wangzhu.vo.StudentVo; @Component
public class StudentDateSet {
private static List<StudentVo> list; static {
StudentDateSet.list = new ArrayList<StudentVo>(); for (int i = 0; i <; i++) {
StudentVo vo = new StudentVo(i, "name" + i, Math.round(100.0f),
"desc" + i, (i & 1) == 0);
StudentDateSet.list.add(vo);
}
} public static List<StudentVo> getList() {
return StudentDateSet.list;
} }
Web Service的接口StudentService.java
package com.wangzhu.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 com.wangzhu.vo.StudentListVo;
import com.wangzhu.vo.StudentVo; @Path("/studentService")
@Produces(MediaType.APPLICATION_JSON)
public interface StudentService { @GET
@Path("/status")
public String getStatus(); @GET
@Path("/students/{index}")
public StudentVo getStudentById(@PathParam("index") Integer id); @GET
@Path("/students")
public StudentListVo getStudentList();
}
Web Service的实现类StudentServiceImpl.java
package com.wangzhu.service.impl; import java.util.List; import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.wangzhu.dataset.StudentDateSet;
import com.wangzhu.service.StudentService;
import com.wangzhu.vo.StudentListVo;
import com.wangzhu.vo.StudentVo; @Component("studentService")
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDateSet studentDateSet; /**
* url:http://<host>:<port>/<appcontext>/services/studentService/status
*/
@Override
@GET
@Path("/status")
public String getStatus() {
return "getStatus";
} /**
* url:http://<host>:<port>/<appcontext>/services/studentService/students/{
* index}
*/
@Override
@GET
@Path("/students/{index}")
public StudentVo getStudentById(@PathParam("index") Integer id) {
List<StudentVo> studentList = StudentDateSet.getList();
StudentVo vo = null;
if (studentList.size() > id) {
vo = studentList.get(id - 1);
}
return vo;
} /**
* url:http://<host>:<port>/<appcontext>/services/studentService/students
*/
@Override
@GET
@Path("/students")
public StudentListVo getStudentList() {
List<StudentVo> studentList = StudentDateSet.getList();
StudentListVo listVo = new StudentListVo(studentList);
return listVo;
} }
备注:注意以上有标记的studentService与applicationWebService.xml中有同名的标记。
测试:
在浏览器的地址栏中输入:
1、http://<host>:<port>/<appcontext>/services/studentService/students
2、http://<host>:<port>/<appcontext>/services/studentService/students/1
使用CXF与Spring集成实现RESTFul WebService的更多相关文章
- 使用Apache CXF和Spring集成创建Web Service(zz)
使用Apache CXF和Spring集成创建Web Service 您的评价: 还行 收藏该经验 1.创建HelloWorld 接口类 查看源码 打印? 1 package ...
- CXF+Spring+Hibernate实现RESTful webservice服务端实例
1.RESTful API接口定义 /* * Copyright 2016-2017 WitPool.org All Rights Reserved. * * You may not use this ...
- Spring集成XFire开发WebService
Spring是眼下最流行的JavaEE Framework,可是使用Spring的Spring-WS开发WebService却十分繁琐.XFire是一个简化WebService开发的开源项目.通过Sp ...
- Spring Boot 实现RESTful webservice服务端实例
1.Spring Boot configurations application.yml spring: profiles: active: dev mvc: favicon: enabled: fa ...
- Spring Boot 实现RESTful webservice服务端示例
1.Spring Boot configurations application.yml spring: profiles: active: dev mvc: favicon: enabled: fa ...
- CXF框架介绍及Spring集成
1.CXF框架概念介绍 Apache CXF 是一个开源的 WebService 框架,CXF可以用来构建和开发 WebService,这些服务可以支持多种协议,比如:SOAP.POST/HTTP.H ...
- RESTful Webservice
1,REST和RESTFUL是什么? REST ( REpresentational State Transfer ),State Transfer 为 "状态传输" 或 &quo ...
- spring 3.0系统集成webservice
spring 3.0系统集成webservice,踩了很多坑以后总算成功了,故写下这篇博客以记录. 1.准备jar包 由于项目是spring3.0,所以应该要使用cxf 2.7版本才可以成功配置,高版 ...
- SSH集成cxf 发布restful webservice
首先讲一下什么是restful webservice ,这个问题网上一搜有很多博文去长篇大论的介绍它,但是最后你看完了也会觉得云里雾里的,所以我在这里简单的讲一下我理解的rest webservice ...
随机推荐
- NSS_04 extjs中grid接收datetime类型参数列
今天在做用户列表时发现, asp.net mvc3的控制器在返回JsonResult结果时, 会把对象内的DateTime类型成员,解析为类似于\/Date(1238606590509)\/的格式 , ...
- PHP利用socket_bind函数切换IP地址采集数据
在利用PHP进行数据采集的过程中,通常会遇到IP被屏蔽或出现验证码的情况:为了能够继续采集,我们需要切换不同的ip,每访问一次,随机切换一个IP.当然也可以通过收集大量代理,通过切换代理的方式进行采集 ...
- lighttpd的超时参数详解
今天服务器上传大文件,服务器php一直没有响应,响应为0KB,经排查发现是lighttpd的超时设置问题 server.max-keep-alive-idle = 5server.max-read-i ...
- dedecms 处理分页样式及去掉分页li
最近装了个织梦dedecmsV5.7版本时,调用分页显示出现的结果出现好几行,怎么也不能在一排显示,找了很多资料,才了解到是由织梦模板中分页加了<Li>列表标签,解决有两种方法,下面将一一 ...
- RichTextBox 自动滚动到最后
RichTextBox.AppendText($"[{DateTime.Now.ToString("hh:mm:ss")}] {msg}\n"); RichTe ...
- JAVA读取TXT文本中的数据
现在在Demo.txt中存在数据: ABC 需要将ABC从文本文件中读取出来 代码片: import java.io.*; class FileReaderDemo { public static v ...
- [原创]PostgreSQL Plus Advince Server在 HA环境中一对多的Stream Replication配置(二)
三.配置主机与备机的ssh无密码登录1.主机s1到备机s3的无密码登录a.创建ssh目录[root@s1 ~]# mkdir /opt/PostgresPlus/9.2AS/.sshb.修改ssh目录 ...
- Eclipse中查看JDK类库的源代码
在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> "Preferences" -> "Java" -> & ...
- cocos2dx中的坐标体系
1.UI坐标系和GL坐标系 2.本地坐标与世界坐标 本地坐标是一个相对坐标,是相对于父节点或者你指明的某个节点的相对位置来说的,本地坐标的原点在参考节点的左下角 世界坐标是一个绝对的坐标,是以屏幕的左 ...
- Qt websocket协议的实现
handshake(握手) client请求: GET /chat HTTP/1.1 Host: server.example.com Upgrade: ...