diff --git a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java index 350e11a2aba0..15130fec49d0 100644 --- a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java +++ b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -68,6 +68,7 @@ * * @author Arjen Poutsma * @author Brian Clozel + * @author Ricardo Figueroa * @author Sebastien Deleuze * @since 3.0.2 * @param the body type @@ -501,6 +502,15 @@ public interface BodyBuilder extends HeadersBuilder { */ BodyBuilder contentType(MediaType contentType); + /** + * Set the {@linkplain ContentDisposition content disposition} of the body, as specified by the + * {@code Content-Disposition} header. + * @param contentDisposition the content disposition + * @return this builder + * @see HttpHeaders#setContentDisposition(ContentDisposition) + */ + BodyBuilder contentDisposition(ContentDisposition contentDisposition); + /** * Set the body of the response entity and returns it. * @param the type of the body @@ -567,6 +577,12 @@ public BodyBuilder contentType(MediaType contentType) { return this; } + @Override + public BodyBuilder contentDisposition(ContentDisposition contentDisposition) { + this.headers.setContentDisposition(contentDisposition); + return this; + } + @Override public BodyBuilder eTag(@Nullable String etag) { if (etag != null) { diff --git a/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java b/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java index 7c52e12edf19..f9f0457afe80 100644 --- a/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java +++ b/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ /** * @author Arjen Poutsma * @author Marcel Overdijk + * @author Ricardo Figueroa * @author Kazuki Shimizu * @author Sebastien Deleuze */ @@ -188,6 +189,10 @@ void headers() { URI location = URI.create("location"); long contentLength = 67890; MediaType contentType = MediaType.TEXT_PLAIN; + ContentDisposition contentDisposition = ContentDisposition. + attachment(). + filename("filename.jpg"). + build(); ResponseEntity responseEntity = ResponseEntity.ok(). allow(HttpMethod.GET). @@ -195,7 +200,8 @@ void headers() { location(location). contentLength(contentLength). contentType(contentType). - headers(headers -> assertThat(headers).hasSize(5)). + contentDisposition(contentDisposition). + headers(headers -> assertThat(headers).hasSize(6)). build(); assertThat(responseEntity).isNotNull(); @@ -207,6 +213,7 @@ void headers() { assertThat(responseHeaders.getFirst(HttpHeaders.LOCATION)).isEqualTo(location.toASCIIString()); assertThat(responseHeaders.getFirst(HttpHeaders.CONTENT_LENGTH)).isEqualTo(String.valueOf(contentLength)); assertThat(responseHeaders.getFirst(HttpHeaders.CONTENT_TYPE)).isEqualTo(contentType.toString()); + assertThat(responseHeaders.getFirst(HttpHeaders.CONTENT_DISPOSITION)).isEqualTo("attachment; filename=\"filename.jpg\""); assertThat(responseEntity.getBody()).isNull(); }