您的位置:首页 > 编程语言 > Java开发

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

2017-02-13 17:12 916 查看
问题:

最近在原有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/110002/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/110002/pieces] is: -1
[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的注入和使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐