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


    问题:

    最近在原有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的注入和使用。

  • 相关阅读:
    记一次ORACLE的UNDO表空间爆满分析过程
    ORACLE AWR报告数据的导入导出实践
    Linux 平台MySQL启动关闭方式总结
    ORACLE 查看RMAN的备份信息总结
    ORACLE AWR报告生成过程出现多个实例记录分析
    ORACLE 如何查看索引重建进度情况
    partproble在RHEL 6下无法更新分区信息
    Oracle行内链接不会引起USER_TABLES中CHAIN_CNT值变化
    Linux服务器宕机案例第二则
    ORA-00600: internal error code, arguments: [kcratr1_lastbwr], [], [], [], [], [], [], []
  • 原文地址:https://www.cnblogs.com/chry/p/6393807.html
Copyright © 2020-2023  润新知