本文介绍了在 Spring-MVC 控制器中触发 404?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 Spring 3.0 控制器触发 404?

How do I get a Spring 3.0 controller to trigger a 404?

我有一个带有 @RequestMapping(value = "/**", method = RequestMethod.GET) 的控制器,还有一些 URLs 访问控制器,我希望容器提供 404.

I have a controller with @RequestMapping(value = "/**", method = RequestMethod.GET) and for some URLs accessing the controller, I want the container to come up with a 404.

推荐答案

自 Spring 3.0 起,您还可以抛出用 @ResponseStatus 注释:

Since Spring 3.0 you also can throw an Exception declared with @ResponseStatus annotation:

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
    ...
}

@Controller
public class SomeController {
    @RequestMapping.....
    public void handleCall() {
        if (isFound()) {
            // whatever
        }
        else {
            throw new ResourceNotFoundException();
        }
    }
}

这篇关于在 Spring-MVC 控制器中触发 404?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:07