通过配置多个DispatcherServlet解决SpringMVC RESTAPI前后端分离资源访问的问题
来源:互联网 发布:js怎么设置transform 编辑:程序博客网 时间:2024/05/23 19:19
起因
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前后端分离资源访问的问题
- 通过配置多个DispatcherServlet解决SpringMVC RESTAPI前后端分离资源访问的问题
- 通过配置多个DispatcherServlet解决SpringMVC RESTAPI前后端分离资源访问的问题
- 通过配置多个DispatcherServlet解决SpringMVC RESTAPI前后端分离资源访问的问题
- SpringMVC 笔记——访问静态资源DispatcherServlet 配置问题
- 前后端分离nginx配置,同时解决跨域问题
- 解决java前后端分离跨域的问题
- 前后端分离问题
- nginx前后端分离的配置
- 前后端分离之后的问题
- Maven多模块,Dubbo分布式服务框架,SpringMVC,前后端分离项目,基础搭建,搭建过程出现的问题
- Maven多模块,Dubbo分布式服务框架,SpringMVC,前后端分离项目,基础搭建,搭建过程出现的问题
- springMVC的dispatcherServlet访问路径映射问题
- 前后端分离,nginx配置解决js、css无法加载
- 通过Nginx访问后端应用,ERR_CONTENT_LENGTH_MISMATCH 问题的解决
- 通过Nginx访问后端应用,ERR_CONTENT_LENGTH_MISMATCH 问题的解决
- 关于Java前后端分离分别部署路径访问的问题
- Nginx 前后端分离配置
- spring boot 前后端分离,解决ajax跨域问题
- php页面显示乱码处理
- PowerDesigner中生成外键FK技巧,powerdesigner导出脚本语言没有外键的处理方法
- 三问微服务
- 文件系统层次结构标准
- 白色相簿2 coda篇各结局概率分析
- 通过配置多个DispatcherServlet解决SpringMVC RESTAPI前后端分离资源访问的问题
- 出现次数最多的整数
- Google Map开发
- kerberos_API_fix
- JavaScript异步初探
- 交叉 编译 openssl
- 常用日期处理方法
- 树状数组 原理及分析
- ListView控件点击无响应