Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestClient ResponseSpec.body() throws an exception instead of returning null if no response body is available #31719

Closed
josephearl opened this issue Nov 29, 2023 · 5 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@josephearl
Copy link

josephearl commented Nov 29, 2023

From RestClient.ResponseSpec:

/**
 * ...
 * @return the body, or {@code null} if no response body was available
 */
@Nullable
<T> T body(Class<T> bodyType);

Calling an API with the following:

restClient
  .get()
  .uri("/api/v1/pokemon/{id}", id)
  .accept(MediaType.APPLICATION_JSON)
  .retrieve()
  .body(Pokemon.class);

If I stub out an endpoint with WireMock as an OK application/json response with no body:

wireMock.stubFor(get(urlEqualTo("/api/v1/pokemon/123")).willReturn(okJson(null)));

I get an exception:

org.springframework.web.client.RestClientException: Error while extracting response for type [com.example.Pokemon] and content type [application/json]
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: No content to map due to end-of-input

Or stubbing it out as just OK:

wireMock.stubFor(get(urlEqualTo("/api/v1/pokemon/123")).willReturn(ok()));

I get different exception:

org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.example.Pokemon] and content type [application/octet-stream]

Expected: null is returned as documented when no response body is returned, at least in the okJson(null) case (I suspect WireMock is defaulting to an octet body in the latter case...)
Alternatively: ResponseSpec.body() is not nullable and is documented as throwing an exception in the case no body is available

Using okJson("null") seems to work and null is returned, but that is an available response body. This is in contrast to WebClient's blockOptional() which did return an empty Optional for okJson(null) and block() which returns null.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 29, 2023
@josephearl

This comment was marked as outdated.

@josephearl

This comment was marked as outdated.

@poutsma poutsma self-assigned this Nov 30, 2023
@poutsma
Copy link
Contributor

poutsma commented Nov 30, 2023

Would you mind putting the code into a sample project that we can unzip or git clone, build, and deploy, and that reproduces the problem?

Never mind, I was able to reproduce it without WireMock.

@poutsma poutsma added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 1, 2023
@poutsma poutsma added this to the 6.1.2 milestone Dec 1, 2023
@poutsma
Copy link
Contributor

poutsma commented Dec 1, 2023

This is related to #12671, so it seems fitting that we adopt the same solution as used for RestTemplate, see b6675b6.

@poutsma poutsma closed this as completed in d204dd2 Dec 1, 2023
@poutsma
Copy link
Contributor

poutsma commented Dec 1, 2023

Fixed, thanks for reporting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants