How to return 404 response status in Spring Boot @ResponseBody - method return type is Response?

后端 未结 4 1137
醉梦人生
醉梦人生 2021-01-30 20:24

I\'m using Spring Boot with @ResponseBody based approach like the following:

@RequestMapping(value = VIDEO_DATA_PATH, method = RequestMethod.GET)
public @Respons         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 20:53

    You can just set responseStatus on res like this:

    @RequestMapping(value = VIDEO_DATA_PATH, method = RequestMethod.GET)
    public ResponseEntity getData(@PathVariable(ID_PARAMETER) long id,
                                                HttpServletResponse res) {
    ...
        res.setStatus(HttpServletResponse.SC_NOT_FOUND); 
        // or res.setStatus(404)
        return null; // or build some response entity
     ...
    }
    

提交回复
热议问题