Spring 3 MVC本身就支持resutful web service, 同时实现Java Resutful Webservice的技术也很多,如Restlet、RestEasy 和Jersey。这里用Jersey来实现。
Jersey的官方介绍:
Jersey is the open source, production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services. But, it is also more than the Reference Implementation.
涉及的技术:
Jersey 1.17
Spring 3.2.2.RELEASE
Maven 3
Flex SDK 4.6
1. 项目依赖Pom.xml片段
2. Web.xml
a.注册Spring “ContextLoaderListener” listener class在web.xml
b.加入Jersey Spring servlet
3. 处理Request请求:
@Component注解定义此类为spring组件,即bean类.
@Autowired指定了一个EmployeeService引用,Spring会提供该引用。
@Path是个JAX-RS注解,它的值是一个相对的URI路径, 所以方法listAllEmployee的http web service的URI就是/CNET/rest/employee/list
@Produces指定了返回的数据格式是json
@GET注解是请求方法指示符,这个指示符注解的Java方法会处理HTTP GET请求
4.Spring的应用上下文 applicationContext.xml
5. EmployeeVO.java
6. Service层和DAO层的具体实现
EmployeeServiceImpl.java
EmployeeDAOImpl.java
5. flex端调用Restful web service
CNET.mxml
访问URL,成功获取数据。
[img]http://dl.iteye.com/upload/attachment/0084/2889/2556a1f0-ef85-38f1-aa09-ebd9f453b767.png[/img]
Jersey的官方介绍:
Jersey is the open source, production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services. But, it is also more than the Reference Implementation.
涉及的技术:
Jersey 1.17
Spring 3.2.2.RELEASE
Maven 3
Flex SDK 4.6
1. 项目依赖Pom.xml片段
......
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<dependencies>
<!-- Jersey -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.17</version>
</dependency>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<!-- Jersey + Spring -->
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.17</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<finalName>CNET</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
...
2. Web.xml
a.注册Spring “ContextLoaderListener” listener class在web.xml
b.加入Jersey Spring servlet
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.cnet.web.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
3. 处理Request请求:
package com.cnet.shared.services;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.cnet.server.service.EmployeeService;
import com.cnet.shared.vo.EmployeeVO;
@Component
@Path("/employee")
public class EmployeeRestService {
@Autowired
EmployeeService service;
@GET
@Path("/list")
@Produces("application/json")
public List<EmployeeVO> listAllEmployee() {
List<EmployeeVO> result = service.listAllEmployee();
return result;
}
}
@Component注解定义此类为spring组件,即bean类.
@Autowired指定了一个EmployeeService引用,Spring会提供该引用。
@Path是个JAX-RS注解,它的值是一个相对的URI路径, 所以方法listAllEmployee的http web service的URI就是/CNET/rest/employee/list
@Produces指定了返回的数据格式是json
@GET注解是请求方法指示符,这个指示符注解的Java方法会处理HTTP GET请求
4.Spring的应用上下文 applicationContext.xml
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.cnet.shared.services" />
<bean id="employeeService" class="com.cnet.server.service.impl.EmployeeServiceImpl" />
<bean id="employeeDAO" class="com.cnet.server.dao.impl.EmployeeDAOImpl" />
</beans>
5. EmployeeVO.java
package com.cnet.shared.vo;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class EmployeeVO implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String userName;
private String emailAddr;
public void setUserName(String name) {
this.userName = name;
}
public String getUserName() {
return this.userName;
}
public void setEmailAddr(String emailAddr) {
this.emailAddr = emailAddr;
}
public String getEmailAddr() {
return this.emailAddr;
}
}
6. Service层和DAO层的具体实现
EmployeeServiceImpl.java
package com.cnet.server.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.cnet.server.dao.EmployeeDAO;
import com.cnet.server.service.EmployeeService;
import com.cnet.shared.vo.EmployeeVO;
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
EmployeeDAO dao;
@Override
public List<EmployeeVO> listAllEmployee() {
return dao.listAllEmployee();
}
}
EmployeeDAOImpl.java
package com.cnet.server.dao.impl;
import java.util.ArrayList;
import java.util.List;
import com.cnet.server.dao.EmployeeDAO;
import com.cnet.shared.vo.EmployeeVO;
public class EmployeeDAOImpl implements EmployeeDAO {
@Override
public List<EmployeeVO> listAllEmployee() {
List <EmployeeVO> list = new ArrayList<EmployeeVO>();
EmployeeVO vo1 = new EmployeeVO();
vo1.setUserName("Ted");
vo1.setEmailAddr("Ted@sun.com");
EmployeeVO vo2 = new EmployeeVO();
vo2.setUserName("Mike");
vo2.setEmailAddr("Mike@sun.com");
list.add(vo1);
list.add(vo2);
return list;
}
}
5. flex端调用Restful web service
CNET.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="webService.send()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
private function onLoad(event : ResultEvent) : void {
var rawData:String = String(event.result);
var rawResult:Object = JSON.parse(rawData);
var arr:Array = rawResult.employeeVO as Array;
var dp:ArrayCollection = new ArrayCollection(arr);
grid.dataProvider = dp;
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService id="webService" method="GET" url="http://localhost:8080/CNET/rest/employee/list" showBusyCursor="true" result="onLoad(event)" />
</fx:Declarations>
<mx:DataGrid id="grid" horizontalCenter="0">
<mx:columns>
<mx:DataGridColumn dataField="userName" headerText="Name" />
<mx:DataGridColumn dataField="emailAddr" width="200" headerText="Email" />
</mx:columns>
</mx:DataGrid>
</s:Application>
访问URL,成功获取数据。
[img]http://dl.iteye.com/upload/attachment/0084/2889/2556a1f0-ef85-38f1-aa09-ebd9f453b767.png[/img]