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

起因

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

静态资源目录如下:

通过配置多个DispatcherServlet解决SpringMVC RESTAPI前后端分离资源访问的问题_第1张图片

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

<servlet>
        <servlet-name>restAppServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:rest-spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet>
        <servlet-name>staticAppServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:static-spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>2load-on-startup>
    servlet>

        
    <servlet-mapping>
        <servlet-name>staticAppServletservlet-name>
        <url-pattern>*.htmlurl-pattern>
    servlet-mapping>
        <servlet-mapping>
        <servlet-name>staticAppServletservlet-name>
        <url-pattern>*.jsurl-pattern>
    servlet-mapping>
        <servlet-mapping>
        <servlet-name>staticAppServletservlet-name>
        <url-pattern>*.cssurl-pattern>
    servlet-mapping>
        <servlet-mapping>
        <servlet-name>staticAppServletservlet-name>
        <url-pattern>*.pngurl-pattern>
    servlet-mapping>

       
    <servlet-mapping>
        <servlet-name>restAppServletservlet-name>
        <url-pattern>/api/*url-pattern>
    servlet-mapping>

rest-spring-mvc.xml


<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 />


    
    <context:component-scan base-package="controller package 的路径" />

    
    <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


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

你可能感兴趣的:(Spring-MVC)