通过配置多个DispatcherServlet解决SpringMVC RESTAPI前后端分离资源访问的问题

起因

Spring MVC项目需要前后端分离,REST API 和 静态资源 需要走不同的DispatcherServlet。

静态资源目录如下:

目录结构

修改web.xml,添加不同的DispatcherServlet

<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前后端分离资源访问的问题

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值