Trigger 404 in Spring-MVC controller?

后端 未结 14 1344
攒了一身酷
攒了一身酷 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:31

    Starting from Spring 5.0, you don't necessarily need to create additional exceptions:

    throw new ResponseStatusException(NOT_FOUND, "Unable to find resource");
    

    Also, you can cover multiple scenarios with one, built-in exception and you have more control.

    See more:

    • ResponseStatusException (javadoc)
    • https://www.baeldung.com/spring-response-status-exception

提交回复
热议问题