Search code examples
javaspringspring-mvchttp-status-codes

Is it possible to return HttpStatus status which is not from HttpStatus enum in Spring MVC


I want to return , 555 status code in response.

I have checked ResponseEntity class of spring framework.

I can see all constructors accept only particular codes from HttpStatus enum. This can be achieved by ,

return ResponseEntity.status(HttpStatus.CREATED).contentType(MediaType.TEXT_PLAIN).body("Custom string answer");

Is there any way to return status code like 555?


Solution

  • You can't do that using the Spring ResponseEntity.

    But you can always get a hold of the underlying HttpServletResponse and do a response.setStatus(555).

    As a side note, if your question was "is it OK to return non-standard HTTP codes in this scenario?", the answer would've probably been "no".