现在的位置: 首页 > 综合 > 正文

Spring REST

2012年10月08日 ⁄ 综合 ⁄ 共 4671字 ⁄ 字号 评论关闭

前面介绍过Spring的MVC结合不同的view显示不同的数据,如:结合json的view显示json、结合xml的view显示xml文档。那么这些数据除了在WebBrowser中用JavaScript来调用以外,还可以用远程服务器的Java程序、C#程序来调用。也就是说现在的程序不仅在BS中能调用,在CS中同样也能调用,不过你需要借助RestTemplate这个类来完成。RestTemplate有点类似于一个WebService客户端请求的模版,可以调用http请求的WebService,并将结果转换成相应的对象类型。至少你可以这样理解!

 

上一次博文介绍SpringMVC结合不同的View,显示不同的数据。http://www.cnblogs.com/hoojo/archive/2011/04/29/2032571.html

 

Email:hoojo_@126.com

Blog:http://blog.csdn.net/IBM_hoojo

http://hoojo.cnblogs.com/

 

一、准备工作

1、 下载jar包

spring各版本jar下载地址:http://ebr.springsource.com/repository/app/library/detail?name=org.springframework.spring

相关的依赖包也可以在这里找到:http://ebr.springsource.com/repository/app/library

 

2、 需要jar包如下

clip_image002

 

3、 当前工程的web.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" 

    xmlns="http://java.sun.com/xml/ns/j2ee" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 

    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    

    <!-- 配置Spring核心控制器 -->

    <servlet>

        <servlet-name>dispatcher</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>/WEB-INF/dispatcher.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

    

    <servlet-mapping>

        <servlet-name>dispatcher</servlet-name>

        <url-pattern>*.do</url-pattern>

    </servlet-mapping>

    

    <welcome-file-list>

      <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

</web-app>

4、 WEB-INF中的dispatcher.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:util="http://www.springframework.org/schema/util"

    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-3.0.xsd

    http://www.springframework.org/schema/mvc

    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

    http://www.springframework.org/schema/context 

    http://www.springframework.org/schema/context/spring-context-3.0.xsd

    http://www.springframework.org/schema/util

    http://www.springframework.org/schema/util/spring-util-3.0.xsd">

 

    <context:component-scan base-package="com.hoo.*">

        <!-- 忽略这个类 -->

        <context:exclude-filter type="assignable" expression="com.hoo.client.RESTClient"/>

    </context:component-scan>

 

    <!-- annotation的方法映射适配器 -->

    <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    

    <!-- xml视图,XStreamMarshaller,可以转换任何形式的java对象 -->

    <bean name="xStreamMarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView">

        <property name="marshaller">

            <bean class="org.springframework.oxm.xstream.XStreamMarshaller">  

                <!--  为了初始化XStreamMarshaller,这个类会把我们接口中得到结果以XML文档形式展现出来 -->

                <property name="autodetectAnnotations" value="true"/>  

            </bean>  

        </property>

    </bean>

        

    <!-- 视图解析器,根据视图的名称new ModelAndView(name),在配置文件查找对应的bean配置 -->

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">

        <property name="order" value="3"/>

    </bean>

 

    <!--  annotation默认的方法映射适配器 -->

    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

        <property name="order" value="1" />

    </bean>

    

</beans>

5、 启动后,可以看到index.jsp 没有出现异常或错误。那么当前SpringMVC的配置就成功了。

 

二、REST控制器实现

REST控制器主要完成CRUD操作,也就是对于http中的post、get、put、delete。

还有其他的操作,如head、options、trace。

具体代码:

package com.hoo.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

 

/**

 * <b>function:</b>SpringMVC REST示例

 * @author hoojo

 * @createDate 2011-6-9 上午11:34:08

 * @file RESTController.java

 * @package com.hoo.controller

 * @project SpringRestWS

 * @blog http://blog.csdn.net/IBM_hoojo

 * @email hoojo_@126.com

 * @version 1.0

 */

@RequestMapping("/restful")

@Controller

public class RESTController {

    

    @RequestMapping(value = "/show", method = RequestMethod.GET)

    public ModelAndView show() {

        System.out.println("show");

        ModelAndView model = new ModelAndView("xStreamMarshallingView");

        model.addObject("show method");

        return model; 

    }

    

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)

    public ModelAndView getUserById(@PathVariable String id) {

        System.out.println("getUserById-" + id);

        ModelAndView model = new ModelAndView("xStreamMarshallingView");

        model.addObject("getUserById method -" + id);

        return model; 

    }

    

    @RequestMapping(value = "/add", method = RequestMethod.POST)

    public ModelAndView addUser(String user) {

        System.out.println("addUser-" + user);

        ModelAndView model = new ModelAndView("xStreamMarshallingView");

        model.addObject("addUser method -" + user);

抱歉!评论已关闭.