diff --git a/build.gradle b/build.gradle index 59446a8d986..4e64d291fe0 100644 --- a/build.gradle +++ b/build.gradle @@ -69,6 +69,7 @@ configure(allprojects) { project -> ext.okhttpVersion = "2.7.5" ext.okhttp3Version = "3.4.1" ext.poiVersion = "3.14" + ext.protobufVersion = "3.0.0-beta-4" ext.reactivestreamsVersion = "1.0.0" ext.reactorVersion = "2.0.8.RELEASE" ext.reactorCoreVersion = '3.0.0.BUILD-SNAPSHOT' @@ -734,7 +735,8 @@ project("spring-web") { optional("org.eclipse.jetty:jetty-server:${jettyVersion}") { exclude group: "javax.servlet", module: "javax.servlet-api" } - optional("com.google.protobuf:protobuf-java:2.6.1") + optional("com.google.protobuf:protobuf-java:${protobufVersion}") + optional("com.google.protobuf:protobuf-java-util:${protobufVersion}") optional("com.googlecode.protobuf-java-format:protobuf-java-format:1.4") optional("javax.mail:javax.mail-api:${javamailVersion}") testCompile(project(":spring-context-support")) // for JafMediaTypeFactory 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 123da86926f..d26afc54ae3 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 @@ -17,20 +17,21 @@ package org.springframework.http.converter.protobuf; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.OutputStreamWriter; import java.lang.reflect.Method; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.concurrent.ConcurrentHashMap; +import com.google.protobuf.CodedOutputStream; import com.google.protobuf.ExtensionRegistry; import com.google.protobuf.Message; import com.google.protobuf.TextFormat; -import com.googlecode.protobuf.format.HtmlFormat; -import com.googlecode.protobuf.format.JsonFormat; +import com.googlecode.protobuf.format.FormatFactory; import com.googlecode.protobuf.format.ProtobufFormatter; -import com.googlecode.protobuf.format.XmlFormat; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; @@ -38,18 +39,28 @@ import org.springframework.http.MediaType; import org.springframework.http.converter.AbstractHttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; -import org.springframework.util.FileCopyUtils; +import org.springframework.util.ClassUtils; /** * An {@code HttpMessageConverter} that reads and writes {@link com.google.protobuf.Message}s * using Google Protocol Buffers. * - *

By default, it supports {@code "application/x-protobuf"}, {@code "text/plain"}, - * {@code "application/json"}, {@code "application/xml"}, while also writing {@code "text/html"}. + *

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

Other formats can be supported with additional libraries: + *

* *

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

Requires Protobuf 2.6 and Protobuf Java Format 1.4, as of Spring 4.3. + * Supports up to Protobuf 3.0.0. * * @author Alex Antonov * @author Brian Clozel @@ -66,18 +77,32 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter, Method> methodCache = new ConcurrentHashMap<>(); private final ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); + static { + if (isProtobufJavaFormatPresent) { + SUPPORTED_MEDIATYPES = new MediaType[] {PROTOBUF, MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, + MediaType.APPLICATION_JSON}; + } + else if (isProtobufJavaUtilPresent) { + SUPPORTED_MEDIATYPES = new MediaType[] {PROTOBUF, MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON}; + } + else { + SUPPORTED_MEDIATYPES = new MediaType[] {PROTOBUF, MediaType.TEXT_PLAIN}; + } + } /** * Construct a new instance. @@ -91,7 +116,16 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter