Spring3.0实现REST实例

Spring3.0实现REST实例

这是一个rest风格的访问,Spring从3.0开始将全面支持rest。不得不感叹Spring的强悍。

项目结构:

第一步永远是配置,使用框架永远都是先有配置,在web.xml中的配置:

[xhtml] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  7. <display-name></display-name>
  8. <context-param>
  9. <!--rest配置文件的路径,貌似不配置也是加载这个地址,这个地方有点疑问,大家指点指点-->
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>/WEB-INF/rest-servlet.xml</param-value>
  12. </context-param>
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16. <servlet>
  17. <!-- 配置一个Servlet,有这个Servlet统一调度页面的请求 -->
  18. <servlet-name>rest</servlet-name>
  19. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  20. <load-on-startup>2</load-on-startup>
  21. </servlet>
  22. <servlet-mapping>
  23. <!-- 映射路径,不要写成了/*那样会拦截所有的访问,连JSP页面都访问不了 -->
  24. <servlet-name>rest</servlet-name>
  25. <url-pattern>/</url-pattern>
  26. </servlet-mapping>
  27. <welcome-file-list>
  28. <welcome-file>/index.jsp</welcome-file>
  29. </welcome-file-list>
  30. </web-app>

第二步:配置rest-servlet.xml这个文件

[xhtml] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
  7. default-lazy-init="true">
  8. <description>Spring公共配置</description>
  9. <!--检测注解-->
  10. <context:component-scan base-package="com.liqiu" />
  11. <bean   class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
  12. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  13. <!-- 注册视图解析器,说白了就是根据返回值指定到某个页面 -->
  14. <bean id="viewResolver"   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  16. <property name="prefix" value="/"></property> <!--页面文件的路径,在根目录下-->
  17. </bean>
  18. </beans>

第三步:具体实现类

[java] view plaincopy

  1. package com.liqiu.controller;
  2. import java.io.IOException;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. @Controller
  10. @RequestMapping("/simple")
  11. public class SimpleController {
  12. //映射路径/simple/index当访问这个路径时,执行这个方法
  13. @RequestMapping("/index")
  14. public String index(HttpServletRequest request ,HttpServletResponse response){
  15. //response,request会自动传进来
  16. request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!");
  17. return "index.jsp";
  18. }
  19. //根据ID获取不同的内容,通过@PathVariable 获得属性
  20. @RequestMapping(value="/{id}",method=RequestMethod.GET)
  21. public String get(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{
  22. request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!<br/>ID:"+id+"");
  23. //response.getWriter().write("You put id is : "+id);
  24. return "index.jsp";
  25. //return null;
  26. }
  27. }

index.jsp页面:

[xhtml] view plaincopy

  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <html>
  3. <head>
  4. <title>Spring3 RESTful</title>
  5. </head>
  6. <body>
  7. ${message}
  8. </body>
  9. </html>

在浏览器中输入:http://localhost:8080/SpringREST/simple/index/,就可以看到效果。

也可以在页面输入不同的参数,获得不同的内容,输入地址:http://localhost:8080/SpringREST/simple/88888,这次执行的就是get方法,通过注解获取ID值,效果:

关于Spring rest 对于Ajax的支持,其实响应Ajax就是通过response返回一个字符串到页面,既然能获得response对象,那问题就迎刃而解了,我们改造下get方法:

[java] view plaincopy

  1. @RequestMapping(value="/{id}",method=RequestMethod.GET)
  2. public String get(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{
  3. //request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!<br/>ID:"+id+"");
  4. response.getWriter().write("You put id is : "+id);
  5. //return "index.jsp";
  6. return null;
  7. }

改造index.jsp页面:

[xhtml] view plaincopy

  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <html>
  3. <head>
  4. <title>Spring3 RESTful</title>
  5. <SCRIPT TYPE="text/javascript">
  6. function go(value){
  7. var url = "/SpringREST/simple/"+value+"/";
  8. var request =  new XMLHttpRequest();
  9. request.open("GET", url, true);
  10. request.setRequestHeader("Content-Type","application/x-javascript;");
  11. request.onreadystatechange = function() {
  12. if (request.readyState == 4) {
  13. if (request.status == 200){
  14. if (request.responseText) {
  15. document.getElementById("text").innerHTML = request.responseText;
  16. }
  17. }
  18. }
  19. };
  20. request.send(null);
  21. }
  22. </SCRIPT>
  23. </head>
  24. <body>
  25. ${message}
  26. <br>
  27. Input the id of you will access object:<input id="id" type="text" size="7"><input type="button" value="Go" onclick="go(document.getElementById(‘id‘).value)">
  28. <div id="text"></div>
  29. </body>
  30. </html>

访问http://localhost:8080/SpringREST/simple/index/,在页面里的输入框中输入值,可以看到返回的数据:

DEMO下载:http://download.csdn.net/source/3383568

时间: 2024-08-02 15:11:55

Spring3.0实现REST实例的相关文章

整合Struts2.2+Spring3.0

2014-08-08 学习李刚老师的j2ee整合struts2+spring3 JAR包链接 http://download.csdn.net/detail/u010393809/7732235 项目outline 1.引入JAR包,上面已经贴了JAR包下载链接 2.配置Struts2,只需要引入struts2必需的那几个包,此时暂时不要引入struts2-spring-plugin-2.2.1.jar,不然会抛出javaPointerNull的异常 配置web.xml; 配置struts.xm

Spring3.0 AOP 详解

一.什么是 AOP. AOP(Aspect Orient Programming),也就是面向切面编程.可以这样理解,面向对象编程(OOP)是从静态角度考虑程序结构,面向切面编程(AOP)是从动态角度考虑程序运行过程. 二.AOP 的作用. 常常通过 AOP 来处理一些具有横切性质的系统性服务,如事物管理.安全检查.缓存.对象池管理等,AOP 已经成为一种非常常用的解决方案. 三.AOP 的实现原理. 如图:AOP 实际上是由目标类的代理类实现的.AOP 代理其实是由 AOP 框架动态生成的一个

Spring3.0官网文档学习笔记(七)--3.4.2

3.4.2 依赖与配置的细节 3.4.2.1  Straight values (primitives, Strings, and so on) JavaBeans PropertyEditors被用来转换这些value到实际的类型.? <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <

开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3

一:项目下载地址(点击 Source code(zip)) https://github.com/fzxblgong/frame_2014-12-15/releases 版本:v1.2大小:20M 二:ssm(mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3) version v1.3 功能 新增:+8.框架在支持mybatis-3.2.8基础上又整合进hibernate4,并支持注释.+9.使用注释ssh方式实现JqueryMiniUi多选树.实例

Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子

转自:http://www.blogjava.net/wangxinsh55/archive/2011/07/24/354925.html Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8整合例子(附完整的请假流程例子,jbpm基础,常见问题解决) Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子(附完整的请假流程例子). 1.       jbpm4.4 测试环境搭建 2.       Jbpm4

android4.0 USB Camera实例(五补充)jpg压缩

前一篇最后 我们说了一个直接将yuv转成jpg的函数 但是转换没有成功 原函数是yuv420转jpg的 研究了下发现 yuv420隔行扫描的的序列是这样的 YYYY YYYY UVUV 而yuv422的隔行扫描的序列是这样的 YU YV YU YV YU YV 所以将函数作如下修改 static int put_jpeg_yuv420p_memory(unsigned char *dest_image, unsigned char *input_image, int width, int hei

android4.0 USB Camera实例(四)CMOS

上一篇说了下usb camera uvc标准的 顺便把CMOS做到一起 操作上基本一至 上一篇HAL层里我已经提供了CMOS的相关接口 JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_yuvtorgb 如果使用和UVC一样的处理 图像显示不出来 所以用另外一种方法 同时这里使用的是斯道ICOOL210开发板测试的 如果使用CMOS还需要修改一些地方 HAL层修改如下 首先增加一个函数如下 int select_input(int input

Spring3.0第三讲:Spring实现简单的登录

学习Spring这些技术性框架,光掌握理论知识是远远不够了,我们要懂得学以致用,用键盘将学到的敲出来,在正确与错误中寻找Spring的用法. 为了给读者一个直观的概念,这里我用Spring搭建一个简单的登录,可以让你很快的了解Spring在持久层.业务层.表现层是怎么运作的,这样后面我们分模块讲解的时候,读者也能很快的知道. 本文所用工具为Eclipse IDE,数据库为Oracle 11g. 首先我们来了解登录这个功能,用户访问登录页面,输入账号和密码,点击登录,后台验证是否有账号和密码匹配,

android4.0 USB Camera实例(六)ffmpeg mpeg编码

前面本来说是做h264编码的 研究了两天发现ffmpeg里的h264编码似乎是要信赖第三方库x264 还是怎么简单怎么来吧所以就整了个mpeg编码 ffmpeg移植前面我有一篇ffmpeg解码里已经给了 具体链接在这http://blog.csdn.net/hclydao/article/details/18546757 怎么使用那里面也已经说了 这里主要是通过ffmpeg将yuv422格式转换成rgb 然后就是yuv422转成mpeg格式 接前面几篇 获取到yuv422数据后 为了能显示出来