首页 > 代码库 > spring-REST

spring-REST

1.controller类:

package com.mzj.practice.controller;import org.apache.log4j.Logger;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; /** * Copyright (C),HANDPAY<br> *  * SpringMVC REST *  * @author muzhongjiang * @date 2014年10月26日 */@RequestMapping("/restful")@Controllerpublic class RESTController {    private final Logger LOG=Logger.getLogger(this.getClass());                @RequestMapping(value = "/show", method = RequestMethod.GET)    public ModelAndView show() {        LOG.info("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) {        LOG.info("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) {        LOG.info("addUser-" + user);        ModelAndView model = new ModelAndView("xStreamMarshallingView");        model.addObject("addUser method -" + user);        return model;     }        @RequestMapping(value = "/edit", method = RequestMethod.PUT)    public ModelAndView editUser(String user) {        LOG.info("editUser-" + user);        ModelAndView model = new ModelAndView("xStreamMarshallingView");        model.addObject("editUser method -" + user);        return model;    }        @RequestMapping(value = "/remove/{id}", method = RequestMethod.DELETE)    public ModelAndView removeUser(@PathVariable String id) {        LOG.info("removeUser-" + id);        ModelAndView model = new ModelAndView("xStreamMarshallingView");        model.addObject("removeUser method -" + id);        return model;    }}

 

2.spring rest 调用:

package com.mzj.practice.rest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.client.RestTemplate;/** * Copyright (C),HANDPAY<br> *  * RestTemplate调用REST资源 *  * RestTemplate的getForObject完成get请求、postForObject完成post请求、put对应的完成put请求、delete完成delete请求;<br> * 还有execute可以执行任何请求的方法, 需要你设置RequestMethod来指定当前请求类型。 <br> * RestTemplate.getForObject(String url, Class<String> responseType, String... urlVariables) * 参数url是http请求的地址,参数Class是请求响应返回后的数据的类型,最后一个参数是请求中需要设置的参数。<br> *  * @author muzhongjiang * @date 2014年10月26日 */@Componentpublic class RESTClient {    @Autowired    private RestTemplate template;    private final static String url = "http://localhost:8080/SpringRestWS/restful/";    public String show() {        return template.getForObject(url + "show.do", String.class, new String[] {});    }    public String getUserById(String id) {        return template.getForObject(url + "get/{id}.do", String.class, id);    }    public String addUser(String user) {        return template.postForObject(url + "add.do?user={user}", null, String.class, user);    }    public String editUser(String user) {        template.put(url + "edit.do?user={user}", null, user);        return user;    }    public String removeUser(String id) {        template.delete(url + "/remove/{id}.do", id);        return id;    }}

 

 3.配置spring-rest.xml:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    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.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd">    <!--    下面配置了xStreamMarshaller是和RESTController中的ModelAndView的view对应的。    因为那边是用xStreamMarshaller进行编组的,所以RestTemplate这边也需要用它来解组。    RestTemplate还指出其他的MarshallingHttpMessageConverter;  --><!--RestTemplate需要配置MessageConvert将返回的xml文档进行转换,解析成JavaObject-->    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">        <property name="messageConverters">            <list>                <bean                    class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">                    <property name="marshaller" ref="xStreamMarshaller" />                    <property name="unmarshaller" ref="xStreamMarshaller" />                </bean>            </list>        </property>    </bean>    <bean id="xStreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">        <property name="annotatedClasses">            <array>            </array>        </property>    </bean></beans>

 

4.测试:

package com.mzj.practice.rest.test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import com.mzj.practice.rest.RESTClient;/** * Copyright (C),HANDPAY<br> *  * RESTClient TEST *  * @author muzhongjiang * @date 2014年10月26日 */@ContextConfiguration("classpath:applicationContext-*.xml")public class RESTClientTest  extends AbstractJUnit4SpringContextTests{    @Autowired    private RESTClient client;    public void testShow() {        System.out.println(client.show());    }    public void testGetUserById() {        System.out.println(client.getUserById("abc"));    }    public void testAddUser() {        System.out.println(client.addUser("jack"));    }    public void testEditUser() {        System.out.println(client.editUser("tom"));    }    public void testRemoveUser() {        System.out.println(client.removeUser("aabb"));    }}

 

spring-REST