Search code examples
spring-bootspring-restcontroller

Content-Length header is removed from HEAD response


We have a REST controller with a HEAD method but no GET method. We don't want the client to be able to retrieve the resource, but we do want them to be able to know whether it exists and how big it is.

@RequestMapping(method = HEAD)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<Void> details() {
    FileMetadata fileMetadata = getFileMetadata();
    return ResponseEntity.ok()
            .headers(responseHeaders -> {
                responseHeaders.setContentLength(fileMetadata.size());
                responseHeaders.setLastModified(fileMetadata.lastModified());
            })
            .build();
}

That was working fine until a few days ago when we upgraded from Spring Boot 3.3.3 to 3.4.0. Now the response has no Content-Length header. I've tried putting breakpoints in the code for various Response classes, but can't find what is removing the header. How can I restore the earlier behaviour and return a response with a Content-Length header?


Solution

  • It is spring boot bug, I've faced such problem in 3.3.4, and it works well with 3.3.7 version. Try latest spring boot version.