Spring MVC 404 Not Found 无异常日志解决方案

Spring MVC 404 Not Found 无错误日志解决方案

Spring MVC  404 Not Found 无异常日志解决方案场景描述,使用Spring MVC 框架,进行数据保存,用firefox的firebug跟踪发现404 Not Found。

 Spring MVC  404 Not Found 无异常日志解决方案Spring MVC  404 Not Found 无异常日志解决方案

 分析:后台没有打印任何错误日志,无法分析问题所在。


解决方案(由我朋友提供)

在spring-mvc.xml 配置文件中 加上一下红色部分的配置文件。Spring MVC  404 Not Found 无异常日志解决方案

Spring MVC  404 Not Found 无异常日志解决方案

在controller 下面添加 ControllerAdviceTest.java 文件(注意:必须是controller下面,因为在spring-mvc.xml 中配置了 component-scan)

Spring MVC  404 Not Found 无异常日志解决方案Spring MVC  404 Not Found 无异常日志解决方案


再次运行 后台输出以下错误日志

Spring MVC  404 Not Found 无异常日志解决方案

很明显表明,对象名称有问题,至此问题解决。附件:附上ControllerAdviceTest.java 源码(不能添加压缩包吗? 请自行修改bag名)

package com.snake.controller;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;

@ControllerAdvice
public class ControllerAdviceTest {

	protected static Logger logger = Logger.getLogger(ControllerAdviceTest.class);
	
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String processUnauthenticatedException(NativeWebRequest request,Exception e) {
    	logger.error(e.getMessage());
        return "viewName"; //返回一个逻辑视图名
    }
}