Trigger 404 in Spring-MVC controller?

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

    Also if you want to return 404 status from your controller all you need is to do this

    @RequestMapping(value = "/somthing", method = RequestMethod.POST)
    @ResponseBody
    public HttpStatus doSomthing(@RequestBody String employeeId) {
        try{
      return HttpStatus.OK;
        } 
        catch(Exception ex){ 
      return HttpStatus.NOT_FOUND;
        }
    }
    

    By doing this you will receive a 404 error in case when you want to return a 404 from your controller.

提交回复
热议问题