在Spring-MVC控制器中触发404? [英] Trigger 404 in Spring-MVC controller?

查看:257
本文介绍了在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)和某些 URL ,我希望容器能够提供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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆