简体   繁体   English

如何在 Spring Boot @ResponseBody 中返回 404 响应状态 - 方法返回类型是 Response?

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

I'm using Spring Boot with @ResponseBody based approach like the following:我将 Spring Boot 与基于 @ResponseBody 的方法一起使用,如下所示:

@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 how to return 404 status
    }
    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());
}

I tried some typical approaches as:我尝试了一些典型的方法:

res.setStatus(HttpStatus.NOT_FOUND.value()); res.setStatus(HttpStatus.NOT_FOUND.value());
new ResponseEntity(HttpStatus.BAD_REQUEST);新的响应实体(HttpStatus.BAD_REQUEST);

but I need to return Response .但我需要返回Response

How to return here 404 status code if video is null?如果视频为空,如何在此处返回 404 状态代码?

This is very simply done by throwing org.springframework.web.server.ResponseStatusException :这是通过抛出org.springframework.web.server.ResponseStatusException非常简单地完成的:

throw new ResponseStatusException(
  HttpStatus.NOT_FOUND, "entity not found"
);

It's compatible with @ResponseBody and with any return value.它与@ResponseBody和任何返回值兼容。 Requires Spring 5+需要春天 5+

Create a NotFoundException class with an @ResponseStatus(HttpStatus.NOT_FOUND) annotation and throw it from your controller.创建一个带有@ResponseStatus(HttpStatus.NOT_FOUND)注释的NotFoundException类并将其从您的控制器中抛出。

@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "video not found")
public class VideoNotFoundException extends RuntimeException {
}

Your original method can return ResponseEntity (doesn't change your method behavior):您的原始方法可以返回ResponseEntity (不会改变您的方法行为):

@RequestMapping(value = VIDEO_DATA_PATH, method = RequestMethod.GET)
public ResponseEntity getData(@PathVariable(ID_PARAMETER) long id, HttpServletResponse res{
... 
}

and return the following:并返回以下内容:

return new ResponseEntity(HttpStatus.NOT_FOUND);

You can just set responseStatus on res like this:您可以像这样在 res 上设置 responseStatus :

@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
 ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM