diff --git a/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java index c8fc030aa298..4fcbf0d34c67 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java @@ -41,7 +41,6 @@ import org.springframework.http.converter.HttpMessageConversionException; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; -import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ConcurrentReferenceHashMap; @@ -55,8 +54,8 @@ * *

To generate {@code Message} Java classes, you need to install the {@code protoc} binary. * - *

This converter supports by default {@code "application/x-protobuf"} and {@code "text/plain"} - * with the official {@code "com.google.protobuf:protobuf-java"} library. + *

This converter supports by default {@code "application/x-protobuf"}, {@code "application/*+x-protobuf"} + * and {@code "text/plain"} with the official {@code "com.google.protobuf:protobuf-java"} library. * The {@code "application/json"} format is also supported with the {@code "com.google.protobuf:protobuf-java-util"} * dependency. See {@link ProtobufJsonFormatHttpMessageConverter} for a configurable variant. * @@ -66,6 +65,7 @@ * @author Brian Clozel * @author Juergen Hoeller * @author Sebastien Deleuze + * @author Kamil Doroszkiewicz * @since 4.1 * @see JsonFormat * @see ProtobufJsonFormatHttpMessageConverter @@ -82,6 +82,11 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter clazz, HttpInputMessage } Message.Builder builder = getMessageBuilder(clazz); - if (PROTOBUF.isCompatibleWith(contentType)) { + if (PROTOBUF.isCompatibleWith(contentType) || + PLUS_PROTOBUF.isCompatibleWith(contentType)) { builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry); } else if (TEXT_PLAIN.isCompatibleWith(contentType)) { @@ -209,14 +215,14 @@ protected void writeInternal(Message message, HttpOutputMessage outputMessage) MediaType contentType = outputMessage.getHeaders().getContentType(); if (contentType == null) { contentType = getDefaultContentType(message); - Assert.state(contentType != null, "No content type"); } Charset charset = contentType.getCharset(); if (charset == null) { charset = DEFAULT_CHARSET; } - if (PROTOBUF.isCompatibleWith(contentType)) { + if (PROTOBUF.isCompatibleWith(contentType) || + PLUS_PROTOBUF.isCompatibleWith(contentType)) { setProtoHeader(outputMessage, message); CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody()); message.writeTo(codedOutputStream); @@ -285,7 +291,7 @@ public ProtobufJavaUtilSupport(JsonFormat.@Nullable Parser parser, JsonFormat.@N @Override public MediaType[] supportedMediaTypes() { - return new MediaType[] {PROTOBUF, TEXT_PLAIN, APPLICATION_JSON}; + return new MediaType[] {PROTOBUF, PLUS_PROTOBUF, TEXT_PLAIN, APPLICATION_JSON}; } @Override diff --git a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java index fe5f0d95f323..31068d79a143 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java @@ -48,12 +48,14 @@ class ProtobufHttpMessageConverterTests { private ExtensionRegistry extensionRegistry = mock(); private Msg testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build(); + private MediaType testPlusProtoMediaType = MediaType.parseMediaType("application/vnd.example.public.v1+x-protobuf"); @Test void canRead() { assertThat(this.converter.canRead(Msg.class, null)).isTrue(); assertThat(this.converter.canRead(Msg.class, ProtobufHttpMessageConverter.PROTOBUF)).isTrue(); + assertThat(this.converter.canRead(Msg.class, this.testPlusProtoMediaType)).isTrue(); assertThat(this.converter.canRead(Msg.class, MediaType.APPLICATION_JSON)).isTrue(); assertThat(this.converter.canRead(Msg.class, MediaType.TEXT_PLAIN)).isTrue(); } @@ -62,6 +64,7 @@ void canRead() { void canWrite() { assertThat(this.converter.canWrite(Msg.class, null)).isTrue(); assertThat(this.converter.canWrite(Msg.class, ProtobufHttpMessageConverter.PROTOBUF)).isTrue(); + assertThat(this.converter.canRead(Msg.class, this.testPlusProtoMediaType)).isTrue(); assertThat(this.converter.canWrite(Msg.class, MediaType.APPLICATION_JSON)).isTrue(); assertThat(this.converter.canWrite(Msg.class, MediaType.TEXT_PLAIN)).isTrue(); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java index 33e2b8008448..52a7e58f86dc 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java @@ -42,12 +42,14 @@ class ProtobufJsonFormatHttpMessageConverterTests { JsonFormat.parser(), JsonFormat.printer()); private final Msg testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build(); + private MediaType testPlusProtoMediaType = MediaType.parseMediaType("application/vnd.examle.public.v1+x-protobuf"); @Test void canRead() { assertThat(this.converter.canRead(Msg.class, null)).isTrue(); assertThat(this.converter.canRead(Msg.class, ProtobufHttpMessageConverter.PROTOBUF)).isTrue(); + assertThat(this.converter.canRead(Msg.class, this.testPlusProtoMediaType)).isTrue(); assertThat(this.converter.canRead(Msg.class, MediaType.APPLICATION_JSON)).isTrue(); assertThat(this.converter.canRead(Msg.class, MediaType.TEXT_PLAIN)).isTrue(); } @@ -56,6 +58,7 @@ void canRead() { void canWrite() { assertThat(this.converter.canWrite(Msg.class, null)).isTrue(); assertThat(this.converter.canWrite(Msg.class, ProtobufHttpMessageConverter.PROTOBUF)).isTrue(); + assertThat(this.converter.canWrite(Msg.class, this.testPlusProtoMediaType)).isTrue(); assertThat(this.converter.canWrite(Msg.class, MediaType.APPLICATION_JSON)).isTrue(); assertThat(this.converter.canWrite(Msg.class, MediaType.TEXT_PLAIN)).isTrue(); }