Skip to content

bodyToMono(byte[].class) corrupts binary response in WebFlux 7.0.1 #36265

@jurgen-camilleri

Description

@jurgen-camilleri

When using Spring WebFlux 7.0.1 (Spring Boot 4.0.2) + JDK 25 to download a PDF (or presumably any binary file) from a REST endpoint, the resulting file is larger than the original and cannot be opened. The same logic works correctly in Spring Boot 2.7.18 + JDK 11 as the file is correctly downloaded and can be opened and viewed.

Steps to reproduce:

  1. Create a REST endpoint returning raw PDF bytes:
public class TestController {

    @GetMapping("/test")
    public ResponseEntity<byte[]> test() throws IOException {
        ClassPathResource file = new ClassPathResource("sample.pdf");
        byte[] data;
        try (InputStream in = file.getInputStream()) {
            data = in.readAllBytes();
        }
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"sample.pdf\"")
                .header(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length))
                .body(data);
    }
}
  1. Use WebClient 7.0.1 to retrieve the file:
Path outputPath = Path.of("sample.pdf");

webClient.get()
        .uri("http://localhost:8080/test")
        .retrieve()
        .bodyToMono(byte[].class)
        .flatMap(bytes -> {
            try {
                Files.write(outputPath, bytes);
                return Mono.empty();
            } catch (IOException e) {
                return Mono.error(e);
            }
        })
        .block();
  1. Compare the saved file with the original. The saved file is larger and cannot be opened.

Expected Behavior:
The downloaded file should match the original byte-for-byte.

Downloading the same file using RestTemplate works fine, on both Spring Boot 2 and 4.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions