Trigger 404 in Spring-MVC controller?

后端 未结 14 1329
攒了一身酷
攒了一身酷 2020-11-29 14:59

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

I have a controller with @RequestMapping(value = \"/**\", method = RequestMethod.GET) and for som

14条回答
  •  执笔经年
    2020-11-29 15:50

    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(); 
            }
        }
    }
    

提交回复
热议问题