Spring-boot: How to set a custom HTTP status code

Created on 26 Apr 2016  路  9Comments  路  Source: spring-projects/spring-boot

Hi,

I am working on an application that is an early prototype and it makes use of the unassigned ranges of the http status code (see http://www.iana.org/assignments/http-status-codes/http-status-codes.xml). When responding back to the REST client with an error, how can I get around the HttpStatus type requirement for creating the ResponseEntity<> object?

@ControllerAdvice
public class GlobalExceptionHandler
{
    @ExceptionHandler(value = { VerifyException.class })
    public ResponseEntity caVerifyException(VerifyException e) throws IOException
    {
         ....
         // ... in this case ... HttpStatus.valueOf(438) ...
         return new ResponseEntity<>(errorDetails, HttpStatus.valueOf(e.getStatusCode()));
         ....
    }

The following error is raised at runtime ...

java.lang.IllegalArgumentException: No matching constant for [438]
    at org.springframework.http.HttpStatus.valueOf(HttpStatus.java:488) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]

How do I set a non-standard custom http status code in this case?

thanks,

-Syed

stackoverflow

Most helpful comment

Thanks for the comments. And I added this here because I felt like this should be supported. The IANA registry is not static. And the experimental codes of today will be slated as standards tomorrow. I don't mind picking up a snapshot build since I am working on a prototype. Much appreciated.

All 9 comments

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

Custom HTTP status codes are currently not supported but in the works for Spring Framework 4.3:
https://jira.spring.io/browse/SPR-14205 ... using the ResponseEntity.status(int) builder method which exists since 4.1 already but doesn't allow non-standard codes to be specified yet.

Thanks for the comments. And I added this here because I felt like this should be supported. The IANA registry is not static. And the experimental codes of today will be slated as standards tomorrow. I don't mind picking up a snapshot build since I am working on a prototype. Much appreciated.

Juergen, any chance we facilitate a constructor ResponseEntity(T body, int statusCode), fashioned after ResponseEntity(T body, HttpStatus statusCode)? It agrees with the manner in which it is often used. Thanks for the consideration. Looking forward to the 4.3 RC2 on 5/4/16.

+1, Would like this feature to be added.

@zhuhw SPR-14205 has been fixed. See this comment for details.

@pengisgood People are more likely to be able to explain the behaviour if you update your Stack Overflow question to provide a complete example that can be used to reproduced the behaviour.

Thanks for your reply @wilkinsona

Actually, I already put a gist in the Stack Overflow question. But anyway, I put the whole demo in the github repo at here: https://github.com/pengisgood/springboot-customize-status-code

Was this page helpful?
0 / 5 - 0 ratings