1. 程式人生 > >通過配置多個DispatcherServlet解決SpringMVC RESTAPI前後端分離資源訪問的問題

通過配置多個DispatcherServlet解決SpringMVC RESTAPI前後端分離資源訪問的問題

起因

spring MVC專案需要前後端分離,REST API 和 靜態資源 需要走不同的DispatcherServlet。

靜態資源目錄如下:

目錄結構

修改web.xml,新增不同的DispatcherServlet


restAppServlet
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:rest-spring-mvc.xml

1


staticAppServlet
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:static-spring-mvc.xml

2

<!-- 靜態資源 Mapping (缺點:對不同格式檔案需要增加配置) -->    
<servlet-mapping>
    <servlet-name>staticAppServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
    <servlet-mapping>
    <servlet-name>staticAppServlet</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
    <servlet-mapping>
    <servlet-name>staticAppServlet</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
    <servlet-mapping>
    <servlet-name>staticAppServlet</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>

<!-- REST API Mapping -->   
<servlet-mapping>
    <servlet-name>restAppServlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet>
        <servlet-name>restAppServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value
>
classpath:rest-spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>staticAppServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:static-spring-mvc.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- 靜態資源 Mapping (缺點:對不同格式檔案需要增加配置) --> <servlet-mapping> <servlet-name>staticAppServlet</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>staticAppServlet</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>staticAppServlet</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>staticAppServlet</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <!-- REST API Mapping --> <servlet-mapping> <servlet-name>restAppServlet</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping>

rest-spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/beans  
             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
             http://www.springframework.org/schema/context  
             http://www.springframework.org/schema/context/spring-context-4.0.xsd  
             http://www.springframework.org/schema/aop  
             http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
             http://www.springframework.org/schema/tx   
             http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
             http://www.springframework.org/schema/mvc    
             http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <!-- 預設的註解對映的支援 -->
    <mvc:annotation-driven />


    <!-- 自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 -->
    <context:component-scan base-package="controller package 的路徑" />

    <!-- 配置Jackson,用於編寫REST API時,將Java Object對映成Json Obejct-->
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <bean class="org.springframework.http.MediaType">
                        <constructor-arg index="0" value="application" />
                        <constructor-arg index="1" value="json" />
                        <constructor-arg index="2" value="UTF-8" />
                </bean>
            </list>
        </property>
    </bean> 

</beans>

static-spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/beans  
             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
             http://www.springframework.org/schema/context  
             http://www.springframework.org/schema/context/spring-context-4.0.xsd  
             http://www.springframework.org/schema/aop  
             http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
             http://www.springframework.org/schema/tx   
             http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
             http://www.springframework.org/schema/mvc    
             http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 配置靜態資源的對映路徑 -->
    <mvc:resources location="/resources/" mapping="/**"/>

</beans>

小結

如果使用預設的配置,Spring MVC DispatcherServlet 會監控 / 路徑下的所有請求(包括了 /api/xxx 和 /index.html),這樣子在靜態資源的訪問上會造成一定的麻煩,也不便於不同資源的維護。通過配置多個DispatcherServlet,將請求分開,就較好的解決了SpringMVC RESTAPI前後端分離資源訪問的問題