4

In Java+Spring application I am using, from a third party called over RestTemplate , we get the error response in the JSON with 200 response code.

e.g

{
    "errors": [{
        "reason": "did not like the request",
        "error": "BAD_REQUEST"
    }]
}

How can I convert BAD_REQUEST to the 400 integer representations. Apache HttpStatus inte does not seem to provide any interface to do so.

Novice User
  • 3,552
  • 6
  • 31
  • 56

2 Answers2

9

Maybe you can use org.springframework.http.HttpStatus:

String error = "BAD_REQUEST";
HttpStatus httpStatus = HttpStatus.valueOf(error);
int errorIntCode = httpStatus.value();

or more safe:

String error = "BAD_REQUEST";
HttpStatus httpStatus = Arrays.stream(HttpStatus.values())
        .filter(status -> status.name().equals(error))
        .findAny()
        .orElse(HttpStatus.INTERNAL_SERVER_ERROR);
int errorIntCode = httpStatus.value();
sergey
  • 368
  • 2
  • 7
-1

More succinct and short

HttpStatus.OK.value();
Jose Mhlanga
  • 805
  • 12
  • 14