Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

@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());
new ResponseEntity(HttpStatus.BAD_REQUEST);

but I need to return Response.

How to return here 404 status code if video is null?

like image 815
catch23 Avatar asked Aug 21 '14 09:08

catch23


People also ask

How do I return a 404 response code?

Open or create the . htaccess-file and enter the relative path to the error page. First though, you have to create the error page (404. html, for example) on the first level of your website (the root-directory).

How do I return HTTP status code in spring boot?

Spring provides a few primary ways to return custom status codes from its Controller classes: using a ResponseEntity. using the @ResponseStatus annotation on exception classes, and. using the @ControllerAdvice and @ExceptionHandler annotations.

How do I change the response status in spring boot?

If we want to specify the response status of a controller method, we need to annotate that method with the @ResponseStatus annotation. It has two interchangeable arguments for the desired response type: code and value. Note, that when we set reason, Spring calls HttpServletResponse. sendError().


2 Answers

This is very simply done by throwing org.springframework.web.server.ResponseStatusException:

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

It's compatible with @ResponseBody and with any return value. Requires Spring 5+

like image 53
andrej Avatar answered Sep 23 '22 20:09

andrej


Create a NotFoundException class with an @ResponseStatus(HttpStatus.NOT_FOUND) annotation and throw it from your controller.

@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "video not found") public class VideoNotFoundException extends RuntimeException { } 
like image 29
chrylis -cautiouslyoptimistic- Avatar answered Sep 22 '22 20:09

chrylis -cautiouslyoptimistic-