Spring3.0实现REST实例

关于REST是什么东西,在这里我就不再多说,大家可以去http://blog.csdn.net/pilou5400/archive/2010/12/24/6096861.aspx看看介绍,直接切入主题:

 

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

      项目结构:

 

 

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

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]--><?xml version="1.0" encoding="UTF-8"?>  

<!--[if !supportLists]-->2.  <!--[endif]--><web-app version="3.0"   

<!--[if !supportLists]-->3.  <!--[endif]-->    xmlns="http://java.sun.com/xml/ns/javaee"   

<!--[if !supportLists]-->4.  <!--[endif]-->    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

<!--[if !supportLists]-->5.  <!--[endif]-->    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   

<!--[if !supportLists]-->6.  <!--[endif]-->    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  

<!--[if !supportLists]-->7.  <!--[endif]-->  <display-name></display-name>   

<!--[if !supportLists]-->8.  <!--[endif]-->  <context-param>  

<!--[if !supportLists]-->9.  <!--[endif]-->        <!--rest配置文件的路径,貌似不配置也是加载这个地址,这个地方有点疑问,大家指点指点-->  

<!--[if !supportLists]-->10.<!--[endif]-->    <param-name>contextConfigLocation</param-name>  

<!--[if !supportLists]-->11.<!--[endif]-->    <param-value>/WEB-INF/rest-servlet.xml</param-value>  

<!--[if !supportLists]-->12.<!--[endif]-->  </context-param>  

<!--[if !supportLists]-->13.<!--[endif]-->  <listener>  

<!--[if !supportLists]-->14.<!--[endif]-->    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

<!--[if !supportLists]-->15.<!--[endif]-->  </listener>  

<!--[if !supportLists]-->16.<!--[endif]-->  <servlet>  

<!--[if !supportLists]-->17.<!--[endif]-->        <!-- 配置一个Servlet,有这个Servlet统一调度页面的请求 -->  

<!--[if !supportLists]-->18.<!--[endif]-->    <servlet-name>rest</servlet-name>  

<!--[if !supportLists]-->19.<!--[endif]-->    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  

<!--[if !supportLists]-->20.<!--[endif]-->    <load-on-startup>2</load-on-startup>  

<!--[if !supportLists]-->21.<!--[endif]-->  </servlet>  

<!--[if !supportLists]-->22.<!--[endif]-->  <servlet-mapping>  

<!--[if !supportLists]-->23.<!--[endif]-->        <!-- 映射路径,不要写成了/*那样会拦截所有的访问,连JSP页面都访问不了 -->  

<!--[if !supportLists]-->24.<!--[endif]-->    <servlet-name>rest</servlet-name>  

<!--[if !supportLists]-->25.<!--[endif]-->    <url-pattern>/</url-pattern>  

<!--[if !supportLists]-->26.<!--[endif]-->  </servlet-mapping>  

<!--[if !supportLists]-->27.<!--[endif]-->  <welcome-file-list>  

<!--[if !supportLists]-->28.<!--[endif]-->    <welcome-file>/index.jsp</welcome-file>  

<!--[if !supportLists]-->29.<!--[endif]-->  </welcome-file-list>  

<!--[if !supportLists]-->30.<!--[endif]--></web-app>  

 

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

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]--><?xml version="1.0" encoding="UTF-8"?>  

<!--[if !supportLists]-->2.  <!--[endif]--><beans xmlns="http://www.springframework.org/schema/beans"  

<!--[if !supportLists]-->3.  <!--[endif]-->    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"  

<!--[if !supportLists]-->4.  <!--[endif]-->    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  

<!--[if !supportLists]-->5.  <!--[endif]-->    xmlns:aop="http://www.springframework.org/schema/aop"  

<!--[if !supportLists]-->6.  <!--[endif]-->    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"  

<!--[if !supportLists]-->7.  <!--[endif]-->    default-lazy-init="true">  

<!--[if !supportLists]-->8.  <!--[endif]-->  

<!--[if !supportLists]-->9.  <!--[endif]-->  <description>Spring公共配置</description>  

<!--[if !supportLists]-->10.<!--[endif]-->  

<!--[if !supportLists]-->11.<!--[endif]-->  <!--检测注解-->  

<!--[if !supportLists]-->12.<!--[endif]-->  <context:component-scan base-package="com.liqiu" />  

<!--[if !supportLists]-->13.<!--[endif]-->  <bean   class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  

<!--[if !supportLists]-->14.<!--[endif]-->  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  

<!--[if !supportLists]-->15.<!--[endif]--><!-- 注册视图解析器,说白了就是根据返回值指定到某个页面 -->      

<!--[if !supportLists]-->16.<!--[endif]-->  <bean id="viewResolver"   class="org.springframework.web.servlet.view.InternalResourceViewResolver">  

<!--[if !supportLists]-->17.<!--[endif]-->    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  

<!--[if !supportLists]-->18.<!--[endif]-->    <property name="prefix" value="/"></property> <!--页面文件的路径,在根目录下-->  

<!--[if !supportLists]-->19.<!--[endif]-->   </bean>  

<!--[if !supportLists]-->20.<!--[endif]--></beans>  

 

 第三步:具体实现类

[java] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->package com.liqiu.controller;  

<!--[if !supportLists]-->2.  <!--[endif]-->  

<!--[if !supportLists]-->3.  <!--[endif]-->import java.io.IOException;  

<!--[if !supportLists]-->4.  <!--[endif]-->  

<!--[if !supportLists]-->5.  <!--[endif]-->import javax.servlet.http.HttpServletRequest;  

<!--[if !supportLists]-->6.  <!--[endif]-->import javax.servlet.http.HttpServletResponse;  

<!--[if !supportLists]-->7.  <!--[endif]-->  

<!--[if !supportLists]-->8.  <!--[endif]-->import org.springframework.stereotype.Controller;  

<!--[if !supportLists]-->9.  <!--[endif]-->import org.springframework.web.bind.annotation.PathVariable;  

<!--[if !supportLists]-->10.<!--[endif]-->import org.springframework.web.bind.annotation.RequestMapping;  

<!--[if !supportLists]-->11.<!--[endif]-->import org.springframework.web.bind.annotation.RequestMethod;  

<!--[if !supportLists]-->12.<!--[endif]-->  

<!--[if !supportLists]-->13.<!--[endif]-->@Controller  

<!--[if !supportLists]-->14.<!--[endif]-->@RequestMapping("/simple")  

<!--[if !supportLists]-->15.<!--[endif]-->public class SimpleController {  

<!--[if !supportLists]-->16.<!--[endif]-->    //映射路径/simple/index当访问这个路径时,执行这个方法  

<!--[if !supportLists]-->17.<!--[endif]-->    @RequestMapping("/index")  

<!--[if !supportLists]-->18.<!--[endif]-->    public String index(HttpServletRequest request ,HttpServletResponse response){  

<!--[if !supportLists]-->19.<!--[endif]-->               //response,request会自动传进来  

<!--[if !supportLists]-->20.<!--[endif]-->        request.setAttribute("message""Hello,This is a example of Spring3 RESTful!");  

<!--[if !supportLists]-->21.<!--[endif]-->        return "index.jsp";  

<!--[if !supportLists]-->22.<!--[endif]-->    }  

<!--[if !supportLists]-->23.<!--[endif]-->    //根据ID获取不同的内容,通过@PathVariable 获得属性  

<!--[if !supportLists]-->24.<!--[endif]-->    @RequestMapping(value="/{id}",method=RequestMethod.GET)  

<!--[if !supportLists]-->25.<!--[endif]-->    public String get(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{  

<!--[if !supportLists]-->26.<!--[endif]-->        request.setAttribute("message""Hello,This is a example of Spring3 RESTful!<br/>ID:"+id+"");  

<!--[if !supportLists]-->27.<!--[endif]-->        //response.getWriter().write("You put id is : "+id);  

<!--[if !supportLists]-->28.<!--[endif]-->        return "index.jsp";  

<!--[if !supportLists]-->29.<!--[endif]-->        //return null;  

<!--[if !supportLists]-->30.<!--[endif]-->    }  

<!--[if !supportLists]-->31.<!--[endif]-->}  

 

   index.jsp页面:

 

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]--><%@ page language="java" pageEncoding="UTF-8"%>  

<!--[if !supportLists]-->2.  <!--[endif]--><html>  

<!--[if !supportLists]-->3.  <!--[endif]-->  <head>  

<!--[if !supportLists]-->4.  <!--[endif]-->    <title>Spring3 RESTful</title>  

<!--[if !supportLists]-->5.  <!--[endif]-->   </head>  

<!--[if !supportLists]-->6.  <!--[endif]-->    

<!--[if !supportLists]-->7.  <!--[endif]-->  <body>  

<!--[if !supportLists]-->8.  <!--[endif]-->    ${message}  

<!--[if !supportLists]-->9.  <!--[endif]-->   </body>  

<!--[if !supportLists]-->10.<!--[endif]--></html>  

 

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

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

<!--[if gte vml 1]><v:shape id="图片_x0020_2" o:spid="_x0000_i1026" type="#_x0000_t75" alt="说明: spring rest id" style='width:484.5pt;height:84pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image003.jpg" o:title="spring rest id"/> </v:shape><![endif]--><!--[if !vml]-->说明: spring rest id<!--[endif]-->

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

 

[java] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->@RequestMapping(value="/{id}",method=RequestMethod.GET)  

<!--[if !supportLists]-->2.  <!--[endif]-->    public String get(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{  

<!--[if !supportLists]-->3.  <!--[endif]-->        //request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!<br/>ID:"+id+"");  

<!--[if !supportLists]-->4.  <!--[endif]-->        response.getWriter().write("You put id is : "+id);  

<!--[if !supportLists]-->5.  <!--[endif]-->        //return "index.jsp";  

<!--[if !supportLists]-->6.  <!--[endif]-->        return null;  

<!--[if !supportLists]-->7.  <!--[endif]-->    }  

    改造index.jsp页面:

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]--><%@ page language="java" pageEncoding="UTF-8"%>  

<!--[if !supportLists]-->2.  <!--[endif]--><html>  

<!--[if !supportLists]-->3.  <!--[endif]-->  <head>  

<!--[if !supportLists]-->4.  <!--[endif]-->    <title>Spring3 RESTful</title>  

<!--[if !supportLists]-->5.  <!--[endif]-->    <SCRIPT TYPE="text/javascript">  

<!--[if !supportLists]-->6.  <!--[endif]-->            function go(value){  

<!--[if !supportLists]-->7.  <!--[endif]-->                var url = "/SpringREST/simple/"+value+"/";  

<!--[if !supportLists]-->8.  <!--[endif]-->                var request =  new XMLHttpRequest();  

<!--[if !supportLists]-->9.  <!--[endif]-->                request.open("GET", url, true);  

<!--[if !supportLists]-->10.<!--[endif]-->                request.setRequestHeader("Content-Type","application/x-javascript;");  

<!--[if !supportLists]-->11.<!--[endif]-->                request.onreadystatechange = function() {  

<!--[if !supportLists]-->12.<!--[endif]-->                    if (request.readyState == 4) {  

<!--[if !supportLists]-->13.<!--[endif]-->                        if (request.status == 200){  

<!--[if !supportLists]-->14.<!--[endif]-->                            if (request.responseText) {  

<!--[if !supportLists]-->15.<!--[endif]-->                                document.getElementById("text").innerHTML = request.responseText;  

<!--[if !supportLists]-->16.<!--[endif]-->                            }  

<!--[if !supportLists]-->17.<!--[endif]-->                        }  

<!--[if !supportLists]-->18.<!--[endif]-->                    }  

<!--[if !supportLists]-->19.<!--[endif]-->                };  

<!--[if !supportLists]-->20.<!--[endif]-->                request.send(null);  

<!--[if !supportLists]-->21.<!--[endif]-->            }  

<!--[if !supportLists]-->22.<!--[endif]-->        </SCRIPT>  

<!--[if !supportLists]-->23.<!--[endif]-->  </head>  

<!--[if !supportLists]-->24.<!--[endif]-->    

<!--[if !supportLists]-->25.<!--[endif]-->  <body>  

<!--[if !supportLists]-->26.<!--[endif]-->    ${message}  

<!--[if !supportLists]-->27.<!--[endif]-->    <br>  

<!--[if !supportLists]-->28.<!--[endif]-->    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)">  

<!--[if !supportLists]-->29.<!--[endif]-->    <div id="text"></div>  

<!--[if !supportLists]-->30.<!--[endif]-->  </body>  

<!--[if !supportLists]-->31.<!--[endif]--></html>  

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

 

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

 

 

你可能感兴趣的:(Spring3)