您的位置:首页 > 其它

Web Services(3)-使用CXF开发REST 服务

2016-03-28 00:00 375 查看
摘要: 1. REST服务介绍
2.使用Spring+CXF发布REST服务
3.总结

1. REST服务介绍

REST 全称是 Representational State Transfer (表述性状态转移), 它是Roy Fielding 博士在2000年写的一篇关于软件架构风格的论文, 简称REST 服务

REST 本质上是使用URL来访问资源的一种方式, 其中包括两部分: 请求方式和请求路径, 比较常见的请求方式是GET与POST但在REST中又有其他类型的请求方式, GET ,POST, PUT,DELETE,HEAD,OPTIONS,REST 是一个“无状态”的架构模式,任何时候可以由客户端发出请求到服务端,最终返回自己想要的数据,也就是说, 服务端将内部资源发布REST服务,客户端通过URL来访问这些资源



2.使用Spring+CXF发布REST服务

第一步:添加maven依赖:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion>

<groupId>demo.ws</groupId>
<artifactId>rest_spring_cxf</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.0.6.RELEASE</spring.version>
<cxf.version>3.0.0</cxf.version>
<jackson.version>2.4.1</jackson.version>
</properties>

<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies></project>

第二步: 配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- CXF -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping></web-app>


第三步: 将接口实现类发布springbean

@Component
public class ProductServiceImpl implements ProductService {

private static final List<Product> productList = new ArrayList<Product>();

static {
productList.add(new Product(1, "iphone6", 6500));
productList.add(new Product(2, "iphone6s", 7000));
}

public List<Product> retrieveAllProducts() {
return productList;
}
}


第四步: 配置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:context="http://www.springframework.org/schema/context"
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.*"/>

<import resource="spring-cxf.xml"/>

</beans>


第五步:REST服务调用:

$(function() {
$.ajax({
type: 'get',
url: 'http://localhost:8080/ws/rest/products',
dataType: 'json',
success: function(data) {
var template = $("#product_table_template").html();
var render = Handlebars.compile(template);
var html = render({
data: data
});
$('#product').html(html);
}
});
});


3.总结

至此, 关于什么是REST服务,如何使用REST服务,与spring集成我们已经学习完了,希望对大家有帮助
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  webservices CXF REST