如何在Spring Boot中返回404响应状态的@ResponseBody - 方法返回类型是Response?
如何在Spring Boot中返回404响应状态的@ResponseBody - 方法返回类型是Response?
我正在使用基于@ResponseBody的Spring Boot方法,代码如下:
@RequestMapping(value = VIDEO_DATA_PATH, method = RequestMethod.GET) public @ResponseBody Response getData(@PathVariable(ID_PARAMETER) long id, HttpServletResponse res) { Video video = null; Response response = null; video = videos.get(id - 1); if (video == null) { // TODO 如何返回404状态码 } serveSomeVideo(video, res); VideoSvcApi client = new RestAdapter.Builder() .setEndpoint("http://localhost:8080").build().create(VideoSvcApi.class); response = client.getData(video.getId()); return response; } public void serveSomeVideo(Video v, HttpServletResponse response) throws IOException { if (videoDataMgr == null) { videoDataMgr = VideoFileManager.get(); } response.addHeader("Content-Type", v.getContentType()); videoDataMgr.copyVideoData(v, response.getOutputStream()); response.setStatus(200); response.addHeader("Content-Type", v.getContentType()); }
我尝试了一些常见的方法,比如:
res.setStatus(HttpStatus.NOT_FOUND.value()); new ResponseEntity(HttpStatus.BAD_REQUEST);
但我需要返回Response。如果视频为空,如何返回404状态码?