Ensure that JSON is written to response body
Issue: SPR-15988
This commit is contained in:
parent
15c82afc1c
commit
b30f6fd991
|
|
@ -356,7 +356,9 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
|||
throws IOException {
|
||||
|
||||
if (contentType.isCompatibleWith(APPLICATION_JSON)) {
|
||||
this.printer.appendTo(message, new OutputStreamWriter(output, charset));
|
||||
OutputStreamWriter writer = new OutputStreamWriter(output, charset);
|
||||
this.printer.appendTo(message, writer);
|
||||
writer.flush();
|
||||
}
|
||||
else {
|
||||
throw new IOException("protobuf-java-util does not support " + contentType + " format");
|
||||
|
|
|
|||
|
|
@ -17,8 +17,10 @@
|
|||
package org.springframework.http.converter.protobuf;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import com.google.protobuf.Message;
|
||||
import com.google.protobuf.util.JsonFormat;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
|
@ -36,6 +38,7 @@ import static org.mockito.Mockito.*;
|
|||
*
|
||||
* @author Alex Antonov
|
||||
* @author Juergen Hoeller
|
||||
* @author Andreas Ahlenstorf
|
||||
*/
|
||||
public class ProtobufHttpMessageConverterTests {
|
||||
|
||||
|
|
@ -104,7 +107,7 @@ public class ProtobufHttpMessageConverterTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void write() throws IOException {
|
||||
public void writeProtobuf() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
|
||||
this.converter.write(this.testMsg, contentType, outputMessage);
|
||||
|
|
@ -121,6 +124,54 @@ public class ProtobufHttpMessageConverterTests {
|
|||
assertEquals("sample.proto", schemaHeader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeJsonWithGoogleProtobuf() throws IOException {
|
||||
this.converter = new ProtobufHttpMessageConverter(
|
||||
new ProtobufHttpMessageConverter.ProtobufJavaUtilSupport(null, null),
|
||||
this.registryInitializer);
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
|
||||
this.converter.write(this.testMsg, contentType, outputMessage);
|
||||
|
||||
assertEquals(contentType, outputMessage.getHeaders().getContentType());
|
||||
|
||||
final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
assertFalse("body is empty", body.isEmpty());
|
||||
|
||||
Msg.Builder builder = Msg.newBuilder();
|
||||
JsonFormat.parser().merge(body, builder);
|
||||
assertEquals(this.testMsg, builder.build());
|
||||
|
||||
assertNull(outputMessage.getHeaders().getFirst(
|
||||
ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER));
|
||||
assertNull(outputMessage.getHeaders().getFirst(
|
||||
ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeJsonWithJavaFormat() throws IOException {
|
||||
this.converter = new ProtobufHttpMessageConverter(
|
||||
new ProtobufHttpMessageConverter.ProtobufJavaFormatSupport(),
|
||||
this.registryInitializer);
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
|
||||
this.converter.write(this.testMsg, contentType, outputMessage);
|
||||
|
||||
assertEquals(contentType, outputMessage.getHeaders().getContentType());
|
||||
|
||||
final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
assertFalse("body is empty", body.isEmpty());
|
||||
|
||||
Msg.Builder builder = Msg.newBuilder();
|
||||
JsonFormat.parser().merge(body, builder);
|
||||
assertEquals(this.testMsg, builder.build());
|
||||
|
||||
assertNull(outputMessage.getHeaders().getFirst(
|
||||
ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER));
|
||||
assertNull(outputMessage.getHeaders().getFirst(
|
||||
ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultContentType() throws Exception {
|
||||
assertEquals(ProtobufHttpMessageConverter.PROTOBUF, this.converter.getDefaultContentType(this.testMsg));
|
||||
|
|
|
|||
Loading…
Reference in New Issue