SpringFramework4系列之SpringMVC:搭建RestFul-註解加XML版

  • requirements

    Mavenjava

    JDK 1.7+web

    Tomcat 8+spring

  • Maven

    maven dependency 須要的包 以下:json

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${project.dependency.spring.core}</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>${project.dependency.javax.servlet}</version>
    <scope>provided</scope>
</dependency>
  • Spring-Config.xml

    須要用到 spring-context 和spring-mvcapi

    namespace以下:spring-mvc

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       ">

    Spring 的配置文件用到 component-scan 和annotation-driven
restful

    <context:component-scan base-package="com.company"/>  是爲了自動將 pojo註冊到 spring 的bean 容器中mvc

    <mvc : annotation-driven/> app

        向 spring 容器中註冊 DefaultAnnotationHandlerMapping。maven

        向 spring 容器中註冊 AnnotationMethodHandlerAdapter。

        具體參考:Spring MVC 中的基於註解的 Controller

<!-- =============================================== -->
<!--             component Scanning                  -->
<!-- =============================================== -->
<context:component-scan base-package="com.company" />
<mvc:annotation-driven />
  • 定義web.xml

<!-- =============================================== -->
<!--             Context value                    -->
<!-- =============================================== -->
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring-config.xml</param-value>
</context-param>

<!-- =============================================== -->
<!--             Filter for Encoding                 -->
<!-- =============================================== -->
<filter>
   <filter-name>encodingFilter</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
   </init-param>
   <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
   </init-param>
</filter>
<filter-mapping>
   <filter-name>encodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- =============================================== -->
<!--             Listener                         -->
<!-- =============================================== -->
<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-config.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>springMVC</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>
  • 構建restful 類

    @Controller變成了@RestController。@RestControllr是spring 4.x中的新註解,@RestController,代表該類的每一個方法返回對象而不是視圖。它實際就是@Controller和 @ResponseBody混合使用的簡寫方法。@ResponseBody的做用是將返回的對象放入響應消息體中

@RestController
public class RestControl {

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String testSearch() {
       return "OK"
    }


  • 接收和返回json

    若是須要接收和返回json格式的數據 須要在 spring的xml裏面 添加Mapping Jackson View 並添加對應的 dependency

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.2.3</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.12</version>
</dependency>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
       <property name="defaultViews">
              <list>
                     <!-- ModelAndView裏的數據變成JSON -->
                     <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
              </list>
       </property>
</bean>

    在 restful 類中 須要使用註解 @RequestBody  和 定義 cosumes 類型

@RestController
public class RestFulClass {


    @RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response test(@RequestBody Request request) {

        response.setCallType("test");
        response.setStatusCode(200);
        response.setStatusMessage("successfully");
        return response;
    }
相關文章
相關標籤/搜索