问题:

最近在原有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. 转:.NET中的三种Timer的区别和用法(转)

    //1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用. System.Windows.Forms.Timer // 2.提供以指定的 ...

  2. swift版的枚举变量

    swift版的枚举变量 swift的枚举类型跟普通的类是极为类似的,使用的时候,请不要以为他是一个常量,以下是测试用源码 // // ViewController.swift // SwiftEnum ...

  3. Allure 安装及使用

    linux下安装方法 Allure requires Java 8 or higher    npm install -g allure-commandline --save-dev (如果npm不能 ...

  4. 【matlab】 幂法 求解最大特征值

    一. 算法: 1.输入矩阵A,初始向量x误差限ep,最大迭代次数N 2.置 k = 1, m1 = 0; 3.求Xr-> norm(x)   abs(Xr)=max[Xi] 1<=i< ...

  5. SIM900A模块HTTP相关调试笔记

    SIM900A模块使用笔记 更新2018-12-8 正常工作状态: 接线方法: 首先将 AT 写入字符串输入框,然后点击 发送.因为模块波特率默认是 9600,所以两条指令的显示都是没有问题的:如果将 ...

  6. NCE3

    Lesson1  A puma at large Pumas are large, cat-like animals which are found in America. When reports ...

  7. codeforces 424D Biathlon Track

    codeforces 424D Biathlon Track 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define f ...

  8. 20165318 预备作业3 Linux安装及学习

    Linux安装及学习 一.VirtualBox和Ubuntu的安装 我安装的是VirtualBox 5.2.6和Ubuntu 16.04 LTS,安装过程按照老师博客中的步骤依次进行,出现了以下几个问 ...

  9. 虚机的SQL Server空间占满之后进行释放的一些操作

    用了好几年的一个虚机,数据库是SQL Server 2008,配额500M. 今天忽然发现无法录入数据,登录后台一看,原来是数据库容量满了. 很久没用SQL Server了,找到一段之前用过的收缩数据 ...

  10. BZOJ3052:[WC2013]糖果公园(树上莫队)

    Description Input Output Sample Input 4 3 51 9 27 6 5 12 33 13 41 2 3 21 1 21 4 20 2 11 1 21 4 2 Sam ...