Merge branch '5.1.x'

master
Rossen Stoyanchev 5 years ago
commit 013249757a
  1. 2
      spring-web/src/main/java/org/springframework/http/HttpHeaders.java
  2. 3
      spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java
  3. 17
      spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java
  4. 5
      src/docs/asciidoc/web/webmvc.adoc

@ -874,7 +874,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
/**
* Set the {@link Locale} of the content language,
* as specified by the {@literal Content-Language} header.
* <p>Use {@code set(CONTENT_LANGUAGE, ...)} if you need
* <p>Use {@code set(CONTENT_LANGUAGE, list)} if you need
* to set multiple content languages.</p>
* @since 5.0
*/

@ -119,7 +119,8 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
if (inputStream instanceof Mono) {
HttpHeaders headers = message.getHeaders();
return Mono.from(body)
return body
.singleOrEmpty()
.switchIfEmpty(Mono.defer(() -> {
headers.setContentLength(0);
return message.setComplete().then(Mono.empty());

@ -33,6 +33,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.MediaType;
@ -136,9 +137,7 @@ public class EncoderHttpMessageWriterTests {
@Test
public void useNegotiatedMediaTypeCharset() {
MediaType negotiatedMediaType = new MediaType("text", "html", ISO_8859_1);
HttpMessageWriter<String> writer = getWriter(TEXT_PLAIN_UTF_8, TEXT_HTML);
writer.write(Mono.just("body"), forClass(String.class), negotiatedMediaType, this.response, NO_HINTS);
@ -148,7 +147,6 @@ public class EncoderHttpMessageWriterTests {
@Test
public void useHttpOutputMessageMediaType() {
MediaType outputMessageMediaType = MediaType.TEXT_HTML;
this.response.getHeaders().setContentType(outputMessageMediaType);
@ -161,16 +159,25 @@ public class EncoderHttpMessageWriterTests {
@Test
public void setContentLengthForMonoBody() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DataBuffer buffer = factory.wrap("body".getBytes(StandardCharsets.UTF_8));
HttpMessageWriter<String> writer = getWriter(Flux.just(buffer), MimeTypeUtils.TEXT_PLAIN);
writer.write(Mono.just("body"), forClass(String.class), TEXT_PLAIN, this.response, NO_HINTS).block();
assertEquals(4, this.response.getHeaders().getContentLength());
}
@Test // gh-22952
public void monoBodyDoesNotCancelEncodedFlux() {
Mono<String> inputStream = Mono.just("body")
.doOnCancel(() -> {
throw new AssertionError("Cancel signal not expected");
});
new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes())
.write(inputStream, forClass(String.class), TEXT_PLAIN, this.response, NO_HINTS)
.block();
}
@Test // SPR-17220
public void emptyBodyWritten() {
HttpMessageWriter<String> writer = getWriter(MimeTypeUtils.TEXT_PLAIN);

@ -4515,8 +4515,7 @@ The following example shows how to achieve the same configuration in XML:
[subs="verbatim"]
----
<mvc:resources mapping="/resources/**" location="/public/">
<mvc:resource-chain>
<mvc:resource-cache/>
<mvc:resource-chain resource-cache="true">
<mvc:resolvers>
<mvc:version-resolver>
<mvc:content-version-strategy patterns="/**"/>
@ -4533,7 +4532,7 @@ bean so that it can be injected into others. You can also make the rewrite trans
rely on `HttpServletResponse#encodeURL`.
Note that, when using both `EncodedResourceResolver` (for example, for serving gzipped or
brotli-encoded resources) and `VersionedResourceResolver`, you must register them in this order.
brotli-encoded resources) and `VersionResourceResolver`, you must register them in this order.
That ensures content-based versions are always computed reliably, based on the unencoded file.
https://www.webjars.org/documentation[WebJars] are also supported through the

Loading…
Cancel
Save