问题:

最近在原有MVC的WEB应用中添加一个REST服务,结果始终报404错误。把 Spring debug 日志打开,发现处理REST请求的Controller已经正确进入

[org.springframework.web.servlet.DispatcherServlet]DispatcherServlet with name 'springMvc' processing GET request for [/sm-web/rest/loanOper//pieces]
[org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping]Mapping [/loanOper/110002/pieces] to HandlerExecutionChain with handler [com.sm.controller.in.IntoPiecesRestController@30630dd] and 1 interceptor
[org.springframework.web.servlet.DispatcherServlet]Last-Modified value for [/sm-web/rest/loanOper//pieces] is: -
[org.springframework.web.bind.annotation.support.HandlerMethodInvoker]Invoking request handler method: public java.util.List com.sm.controller.in.IntoPiecesRestController.getIntoPieceList()

然而, 从Controller调用结束后, 却并没有直接返回我需要的json数据, 而是被MVC继续当作一个view处理, 被交jsp的视图解析器,我的REST URL请求被继续处理, 自动添加了/WEB-INF/view前缀和.jsp后缀。导致404错误。

[org.springframework.web.servlet.DispatcherServlet]Rendering view [org.springframework.web.servlet.view.JstlView: name 'loanOper/110002/pieces'; URL [/WEB-INF/view/loanOper/110002/pieces.jsp]] in DispatcherServlet with name 'springMvc'
[org.springframework.web.servlet.view.JstlView]Added model object 'mapList' of type [java.util.ArrayList] to request in view with name 'loanOper/110002/pieces'
[org.springframework.web.servlet.view.JstlView]Forwarding to resource [/WEB-INF/view/loanOper/110002/pieces.jsp] in InternalResourceView 'loanOper/110002/pieces'
[org.springframework.web.servlet.DispatcherServlet]Successfully completed request

分析:

有人说是需要新添加视图解析器, 但我始终觉得不对, 因为我的REST请求在进入视图解析器之前,已经被处理,我只需要通过@RestController将其自动转换为JSON。而且我之前的项目都是这么做的,完全没有问题。

解决办法:

最后, 我另外写了一个新的工程, 一个简单的HelloWorld。正确, 我一一对比了web.xml和spring配置。 最中发现是现有项目的spring配置上有问题。没有添加<mvc:annotation-driving/>配置 !加上后就没有问题了。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 《》
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.test" />
<mvc:annotation-driven />

<mvc:annotation-driving/>的作用

 <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。


<context:annotation-config/>是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。

Spring MVC的Rest URL 被错误解析成jsp, 导致404错误(XML方式下@Controller和@RestController需要配置<mvc:annotation-driving/>)的更多相关文章

  1. ASP.NET MVC 3 loginUrl自动变成Account/Login,并且发生404错误的解决方法

    http://www.cnblogs.com/think8848/archive/2011/07/08/2100814.html ASP.NET MVC 3 loginUrl自动变成Account/L ...

  2. thinkphp nginx php-fpm url rewrite 导致 404 错误

    ## thinkphp nginx php-fpm url rewrite 导致 404 错误 之前thinkphp的系统部署在apache上,考虑到在并发性能nginx比apache强悍得多,所以在 ...

  3. IIS7.5 URL文件名有加号或空格显示404错误的解决办法

    转:http://www.gyd.cc/zhuanti/tech/9319.html 将服务器由windows2003升级到windows2008后,某个网站的图片突然不能显示,显示404错误, 后来 ...

  4. 转 使用@Controller注解为什么要配置<mvc:annotation-driven />

    <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案.<mvc:annotation-dri ...

  5. 使用@Controller注解为什么要配置<mvc:annotation-driven />

    自己看了官方文档,也到网上查了下,目前理解如下: <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和Annotat ...

  6. Asp.netMVC中地址后缀使用.html,jsp等404错误解决

    asp.net mvc 默认的地址路径url都是没有后缀的比如 www.a.com/aa/bb 等 如果要是www.a.com/aa/bb.html需要专门写路由. 根据我之前的经验,mvc的路由是相 ...

  7. Vue实现远程获取路由与页面刷新导致404错误的解决

    一.背景 先简单介绍一下现在项目情况:前后端分离,后端服务是Java写的,前端是Vue+ElementUI. 最近的一个需求是:通过后端Api去获取前端路由表,原因是每个登录角色对应的前端路由表可能是 ...

  8. url的参数解析成key-value

    function urlController(url) { var _url = url.split("?")[1]; if(!_url){ return {}; } var wi ...

  9. 160331、使用@Controller注解为什么要配置<mvc:annotation-driven />

    为了解决静态资源访问的问题,servlet改成了拦截所有请求,即/,并添加了默认的servlet,这时候*.do请求不能被控制器捕捉了,页面错误为404.直到添加了<mvc:annotation ...

随机推荐

  1. openweathermap-免费的天气预报接口

    openweathermap-免费的天气预报接口 其官方网址为:http://www.openweathermap.org/api 静态截图: 具备以下的一些特点: 1. Current weathe ...

  2. 进程分析之CPU

    进程分析之CPU 进程分析之CPU 本文转载自:https://github.com/ColZer/DigAndBuried/blob/master/system/cpu.md 在<进程分析之内 ...

  3. HTTP 错误 404.3 - Not Found的问题(WCF)

    模块 StaticFileModule 通知 ExecuteRequestHandler 处理程序 StaticFile 错误代码 0x80070032 请求的 URL http://10.101.3 ...

  4. ZT 二叉树先序,中序,后序遍历非递归实现

    二叉树先序,中序,后序遍历非递归实现 分类: 数据结构及算法2012-04-28 14:30 8572人阅读 评论(6) 收藏 举报 structc 利用栈实现二叉树的先序,中序,后序遍历的非递归操作 ...

  5. Spring Boot 以 war 方式部署

    Spring Boot 默认自带了一个嵌入式的 Tomcat 服务器,可以以jar方式运行,更为常见的情况是需要将 Spring Boot 应用打包成一个 war 包,部署到 Tomcat.Jerry ...

  6. Ajax请求:本地跨域的问题

    问题出现一: 1.Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-e ...

  7. 个人技术博客(1/2)android布局技巧

    (1)weight属性的合理应用 当使用match_parent(fill_parent)时,需要经过计算,否则会出现如下情况 代码: <LinearLayout xmlns:android=& ...

  8. .Net WebApi 支持跨域访问使用 Microsoft.AspNet.WebApi.Cors

    首先导入Cors库,通过程序包管理控制台导入 Install-Package Microsoft.AspNet.WebApi.Cors 引用库之后,我们需要进行简单的配置. 现在WebApiConfi ...

  9. Spark系列-初体验(数据准备篇)

    Spark系列-初体验(数据准备篇) Spark系列-核心概念 在Spark体验开始前需要准备环境和数据,环境的准备可以自己按照Spark官方文档安装.笔者选择使用CDH集群安装,可以参考笔者之前的文 ...

  10. 【洛谷】【动态规划/背包】P1833 樱花

    [题目描述:] 爱与愁大神后院里种了n棵樱花树,每棵都有美学值Ci.爱与愁大神在每天上学前都会来赏花.爱与愁大神可是生物学霸,他懂得如何欣赏樱花:一种樱花树看一遍过,一种樱花树最多看Ai遍,一种樱花树 ...