Is this the correct way to return 204 "No Content" using Spring?

12.2k Views Asked by At

I'm trying to delete a resource setting http status NO_CONTENT using Weblogic and the response takes 30 seconds to complete. Am i misusing Spring or is there a bug in Weblogic?

I've tried this on Weblogic 12.2.1.0.0 using Spring 5.0.12. The request is completed, the resource is removed but the client waits for 30 seconds (browser TTFB) after that. This seems to be a kind of timeout.

As a workaround, we could return http status OK.

@DeleteMapping(value = "/{id}")
public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
    useCase.remove(id);
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

I expected the client to immediately receive the response, but it waits 30 seconds.

3

There are 3 best solutions below

0
Karol Dowbecki On BEST ANSWER

Comparing to 200 response your 204 response has no Content-Length header. Try adding it manually to see if it helps with your application server handling empty response body:

return ResponseEntity.noContent().header("Content-Length", "0").build();
0
dehasi On

You can also use @ResponseStatus annotation.

@DeleteMapping(value = "/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public ResponseEntity<Void> delete(@PathVariable("id") Long id) {}
2
devwebcl On

This is a known bug from OHS. https://support.oracle.com/knowledge/Middleware/2162306_1.html

The bug behavior is: the plugin keeps waiting for the content body, and 204 doesn't have a body finally it triggers a read timeout (this is the 30+ secs).

You need to download the patch and apply to OHS, or change the HTTP code as workaround, perhaps a 202 (accepted).