HttpStatusCode in Spring Boot 3
Asked Answered
B

4

6

I just migrated an application to Spring Boot 3 and before the migration I used HttpStatus to obtain more details about the error message that a certain web client returned with the gerReasonPhrase() method.

Now I find that in Spring Boot 3, HttpStatus is deprecated and I have to use HttpStatusCode instead. But HttpStatusCode does not have the getReasonPhrase() method or anything similar. Is there a way to obtain the same information as before through HttpStatusCode without having to make a status code map with its corresponding messages?

I am searching a similar method in Spring Boot 3 than the one that exists in Spring Boot 2.

Boydboyden answered 20/12, 2023 at 16:40 Comment(2)
Which HttpStatus are you talking about? The class org.springframework.http.HttpStatus (Spring Framework 6.1.2; e.g. Spring Boot 3.2.0 uses Spring Framework 6.1.1) doesn't seem to be deprecated. It might be a good idea to provide a minimal reproducible example of the code you're having problems with.Ironclad
Also HttpStatus implements HttpStatusCode (and HttpStatus seems to be the only public implementation of HttpStatusCode), so maybe a conditional cast is sufficient to access it if you're calling some method that returns HttpStatusCode instead of HttpStatus.Ironclad
C
4

I'm copying the top part of this answer Error Response body changed after Boot 3 upgrade

Spring Web 6 introduced support for the "Problem Details for HTTP APIs" specification, RFC 7807

With this, the ResponseStatusException now implements the ErrorResponse interface and extends the ErrorResponseException class.

Having a quick look at the javadocs, we can see that all these are backed by the RFC 7807 formatted ProblemDetail body, which, as you can imagine, has the fields of the new response you're getting, and also uses the application/problem+json media type in the response.

ResponseStatusException.getBody();

returns the ProblemDetail. In the javadoc for setTitle, you'll see the value comes from HttpStatus.getReasonPhrase(). So that's how you upgrade that part to spring-boot3.

HttpStatus.getReasonPhrase(); 

in spring-boot3 becomes

ResponseStatusException.getBody().getTitle();

Also, you can use that ProblemDetail object to get the status code

ResponseStatusException.getBody().getStatus();
Congruency answered 18/1 at 18:56 Comment(0)
A
1

HttpStatus don't seems to be deprecated in the current spring version.

But there are some of the enum values that are deprecated (ex. CHECKPOINT).

Also, HttpStatus actually implements HttpStatusCode, they are not two different implementations. And so you can pass status code between them since its the same value field declared in HttpStatusCode (see mapping)

Andesite answered 20/12, 2023 at 17:7 Comment(0)
B
0

I think, you just need to cast it to (HttpStatus), which has the required method. The mentioned HttpStatusCode is an interface, and the HttpStatus is one of its implemenations.

Banana answered 23/4 at 23:40 Comment(0)
K
0

In my application, I did the following:

Before:

public Mono<MyResponse> executeSomething(final String name, final MyRequest request, final WebClientResponseException exception) {
    log.error("FAILED API - Info: {} and request: {}", name, request, exception);
    return Mono.error(() -> new DependencyException(exception.getStatusCode().value(),
            exception.gerReasonPhrase(), exception.getMessage(), exception));
}

After:

    public Mono<MyResponse> executeSomething(final String name, final MyRequest request, final WebClientResponseException exception) {
    log.error("FAILED API - Info: {} and request: {}", name, request, exception);
    return Mono.error(() -> new DependencyException(exception.getStatusCode().value(),
            exception.getStatusText(), exception.getMessage(), exception));
}

I only replaced getReasonPhrase() with getStatusText().

Kennet answered 27/5 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.