Skip to content

Commit 9d6e720

Browse files
committed
Add content disposition setter to ResponseEntity
1 parent a55207e commit 9d6e720

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

spring-web/src/main/java/org/springframework/http/ResponseEntity.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,6 +68,7 @@
6868
*
6969
* @author Arjen Poutsma
7070
* @author Brian Clozel
71+
* @author Ricardo Figueroa
7172
* @author Sebastien Deleuze
7273
* @since 3.0.2
7374
* @param <T> the body type
@@ -501,6 +502,15 @@ public interface BodyBuilder extends HeadersBuilder<BodyBuilder> {
501502
*/
502503
BodyBuilder contentType(MediaType contentType);
503504

505+
/**
506+
* Set the {@linkplain ContentDisposition content disposition} of the body, as specified by the
507+
* {@code Content-Disposition} header.
508+
* @param contentDisposition the content disposition
509+
* @return this builder
510+
* @see HttpHeaders#setContentDisposition(ContentDisposition)
511+
*/
512+
BodyBuilder contentDisposition(ContentDisposition contentDisposition);
513+
504514
/**
505515
* Set the body of the response entity and returns it.
506516
* @param <T> the type of the body
@@ -567,6 +577,12 @@ public BodyBuilder contentType(MediaType contentType) {
567577
return this;
568578
}
569579

580+
@Override
581+
public BodyBuilder contentDisposition(ContentDisposition contentDisposition) {
582+
this.headers.setContentDisposition(contentDisposition);
583+
return this;
584+
}
585+
570586
@Override
571587
public BodyBuilder eTag(@Nullable String etag) {
572588
if (etag != null) {

spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
/**
2828
* @author Arjen Poutsma
2929
* @author Marcel Overdijk
30+
* @author Ricardo Figueroa
3031
* @author Kazuki Shimizu
3132
* @author Sebastien Deleuze
3233
*/
@@ -188,14 +189,19 @@ void headers() {
188189
URI location = URI.create("location");
189190
long contentLength = 67890;
190191
MediaType contentType = MediaType.TEXT_PLAIN;
192+
ContentDisposition contentDisposition = ContentDisposition.
193+
attachment().
194+
filename("filename.jpg").
195+
build();
191196

192197
ResponseEntity<Void> responseEntity = ResponseEntity.ok().
193198
allow(HttpMethod.GET).
194199
lastModified(12345L).
195200
location(location).
196201
contentLength(contentLength).
197202
contentType(contentType).
198-
headers(headers -> assertThat(headers).hasSize(5)).
203+
contentDisposition(contentDisposition).
204+
headers(headers -> assertThat(headers).hasSize(6)).
199205
build();
200206

201207
assertThat(responseEntity).isNotNull();
@@ -207,6 +213,7 @@ void headers() {
207213
assertThat(responseHeaders.getFirst(HttpHeaders.LOCATION)).isEqualTo(location.toASCIIString());
208214
assertThat(responseHeaders.getFirst(HttpHeaders.CONTENT_LENGTH)).isEqualTo(String.valueOf(contentLength));
209215
assertThat(responseHeaders.getFirst(HttpHeaders.CONTENT_TYPE)).isEqualTo(contentType.toString());
216+
assertThat(responseHeaders.getFirst(HttpHeaders.CONTENT_DISPOSITION)).isEqualTo("attachment; filename=\"filename.jpg\"");
210217

211218
assertThat(responseEntity.getBody()).isNull();
212219
}

0 commit comments

Comments
 (0)