spring4之搭建restful风格的web服务

spring4之搭建restful风格的web服务

通过spring mvc搭建一个提供Restful API的web服务。

准备工作

  • 下载并安装Eclipse ee版本(4.5.2)
  • 下载并安装Tomcat 8.0
  • 下载spring framework 4.3.3

    http://repo.spring.io/release/org/springframework/spring/4.3.3.RELEASE/

  • 下载commons-logging-1.2.jar
  • 下载jackson的三个jar包

    jackson-annotations-2.7.0.jar

    jackson-core-2.7.5.jar

    jackson-databind-2.7.5.jar

Eclipse添加Tomcat

解压tomcat

进入Eclipse首选项,在Server-Runtime Environments添加刚解压的tomcat

创建web工程

  1. 通过Eclipse创建Dynamic Web Project

    工程名:sh

    target runtime选择Apache Tomcat v8.0

    勾选自动生成web.xml

  2. 配置lib

    拷贝spring framework相关jar包、jackson三个jar包、commons-logging到WEB-ING/lib下

  3. 配置web.xml

    添加spring 调度servlet

    <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>/url-pattern>
    servlet-mapping>

    添加encodingFilter

     <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
  4. 配置springmvc-servlet.xml

    在WEB-INF下新建springmvc-servlet.xml,添加如下配置:

    指定spring在哪个报下扫描controller组件

    <context:component-scan base-package="sh.controller" />

    指定jsp路径(该配置和restful没有直接联系属于mvc常规配置)

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    bean>

    指定contoller输出转换器

        <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;charset=UTF-8value>
            list>
        property>
    bean>
    <bean id="jsonConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">bean>
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="stringConverter"/>
                <ref bean="jsonConverter" />
            list>
        property>
     bean>
  5. 编写restful api
    创建包sh.controller放置控制器
    创建包sh.vo放置前端数据对象

    在sh.vo下创建简单的Bean Person.java,用于测试java对象与json间的自动转换

    public class Person {
    private String name;
    private int age;
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return this.age;
    }
    }

    在sh.controller下创建restful的控制器。方法参数加上RequestBody注解才能自动将json格式的数据转为输入对象,ResponseBody注解用来控制返回结果作为输出内容。

    @Controller
    public class HelloController {  
    @RequestMapping(value = "/person", method = RequestMethod.POST)
    public @ResponseBody
    Person addPerson(@RequestBody Person person) {
        return person;
    }
    @RequestMapping(value = "/person", method = RequestMethod.GET)
    public @ResponseBody
    List getAllPerson() {
        List list= new ArrayList();
        Person p = new Person();
        p.setAge(1);
        p.setName("rbg");
        list.add(p);
        return list;
    }
    }

你可能感兴趣的:(spring)