Ensure response not closed by MappingJackson2HttpMessageConverter
Closes gh-25987
This commit is contained in:
parent
37504e75e9
commit
7be7e5beb4
|
|
@ -18,6 +18,7 @@ package org.springframework.http.converter.json;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
@ -55,6 +56,7 @@ import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.util.StreamUtils;
|
||||||
import org.springframework.util.TypeUtils;
|
import org.springframework.util.TypeUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -308,7 +310,8 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
|
||||||
MediaType contentType = outputMessage.getHeaders().getContentType();
|
MediaType contentType = outputMessage.getHeaders().getContentType();
|
||||||
JsonEncoding encoding = getJsonEncoding(contentType);
|
JsonEncoding encoding = getJsonEncoding(contentType);
|
||||||
|
|
||||||
try (JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding)) {
|
OutputStream outputStream = StreamUtils.nonClosing(outputMessage.getBody());
|
||||||
|
try (JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputStream, encoding)) {
|
||||||
writePrefix(generator, object);
|
writePrefix(generator, object);
|
||||||
|
|
||||||
Object value = object;
|
Object value = object;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -47,6 +47,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
import static org.assertj.core.api.Assertions.entry;
|
import static org.assertj.core.api.Assertions.entry;
|
||||||
import static org.assertj.core.api.Assertions.within;
|
import static org.assertj.core.api.Assertions.within;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Jackson 2.x converter tests.
|
* Jackson 2.x converter tests.
|
||||||
|
|
@ -149,6 +151,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||||
assertThat(result.contains("\"bool\":true")).isTrue();
|
assertThat(result.contains("\"bool\":true")).isTrue();
|
||||||
assertThat(result.contains("\"bytes\":\"AQI=\"")).isTrue();
|
assertThat(result.contains("\"bytes\":\"AQI=\"")).isTrue();
|
||||||
assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON);
|
assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON);
|
||||||
|
verify(outputMessage.getBody(), never()).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue