41
41
import org .springframework .http .converter .HttpMessageConversionException ;
42
42
import org .springframework .http .converter .HttpMessageNotReadableException ;
43
43
import org .springframework .http .converter .HttpMessageNotWritableException ;
44
- import org .springframework .util .Assert ;
45
44
import org .springframework .util .ClassUtils ;
46
45
import org .springframework .util .ConcurrentReferenceHashMap ;
47
46
55
54
*
56
55
* <p>To generate {@code Message} Java classes, you need to install the {@code protoc} binary.
57
56
*
58
- * <p>This converter supports by default {@code "application/x-protobuf"} and {@code "text/plain "}
59
- * with the official {@code "com.google.protobuf:protobuf-java"} library.
57
+ * <p>This converter supports by default {@code "application/x-protobuf"}, {@code "application/*+x-protobuf "}
58
+ * and {@code "text/plain"} with the official {@code "com.google.protobuf:protobuf-java"} library.
60
59
* The {@code "application/json"} format is also supported with the {@code "com.google.protobuf:protobuf-java-util"}
61
60
* dependency. See {@link ProtobufJsonFormatHttpMessageConverter} for a configurable variant.
62
61
*
66
65
* @author Brian Clozel
67
66
* @author Juergen Hoeller
68
67
* @author Sebastien Deleuze
68
+ * @author Kamil Doroszkiewicz
69
69
* @since 4.1
70
70
* @see JsonFormat
71
71
* @see ProtobufJsonFormatHttpMessageConverter
@@ -82,6 +82,11 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
82
82
*/
83
83
public static final MediaType PROTOBUF = new MediaType ("application" , "x-protobuf" , DEFAULT_CHARSET );
84
84
85
+ /**
86
+ * The media-type for protobuf {@code application/*+x-protobuf}.
87
+ */
88
+ public static final MediaType PLUS_PROTOBUF = new MediaType ("application" , "*+x-protobuf" , DEFAULT_CHARSET );
89
+
85
90
/**
86
91
* The HTTP header containing the protobuf schema.
87
92
*/
@@ -132,7 +137,7 @@ else if (protobufJsonFormatPresent) {
132
137
}
133
138
134
139
setSupportedMediaTypes (Arrays .asList (this .protobufFormatSupport != null ?
135
- this .protobufFormatSupport .supportedMediaTypes () : new MediaType [] {PROTOBUF , TEXT_PLAIN }));
140
+ this .protobufFormatSupport .supportedMediaTypes () : new MediaType [] {PROTOBUF , PLUS_PROTOBUF , TEXT_PLAIN }));
136
141
137
142
this .extensionRegistry = (extensionRegistry == null ? ExtensionRegistry .newInstance () : extensionRegistry );
138
143
}
@@ -162,7 +167,8 @@ protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage
162
167
}
163
168
164
169
Message .Builder builder = getMessageBuilder (clazz );
165
- if (PROTOBUF .isCompatibleWith (contentType )) {
170
+ if (PROTOBUF .isCompatibleWith (contentType ) ||
171
+ PLUS_PROTOBUF .isCompatibleWith (contentType )) {
166
172
builder .mergeFrom (inputMessage .getBody (), this .extensionRegistry );
167
173
}
168
174
else if (TEXT_PLAIN .isCompatibleWith (contentType )) {
@@ -209,14 +215,14 @@ protected void writeInternal(Message message, HttpOutputMessage outputMessage)
209
215
MediaType contentType = outputMessage .getHeaders ().getContentType ();
210
216
if (contentType == null ) {
211
217
contentType = getDefaultContentType (message );
212
- Assert .state (contentType != null , "No content type" );
213
218
}
214
219
Charset charset = contentType .getCharset ();
215
220
if (charset == null ) {
216
221
charset = DEFAULT_CHARSET ;
217
222
}
218
223
219
- if (PROTOBUF .isCompatibleWith (contentType )) {
224
+ if (PROTOBUF .isCompatibleWith (contentType ) ||
225
+ PLUS_PROTOBUF .isCompatibleWith (contentType )) {
220
226
setProtoHeader (outputMessage , message );
221
227
CodedOutputStream codedOutputStream = CodedOutputStream .newInstance (outputMessage .getBody ());
222
228
message .writeTo (codedOutputStream );
@@ -285,7 +291,7 @@ public ProtobufJavaUtilSupport(JsonFormat.@Nullable Parser parser, JsonFormat.@N
285
291
286
292
@ Override
287
293
public MediaType [] supportedMediaTypes () {
288
- return new MediaType [] {PROTOBUF , TEXT_PLAIN , APPLICATION_JSON };
294
+ return new MediaType [] {PROTOBUF , PLUS_PROTOBUF , TEXT_PLAIN , APPLICATION_JSON };
289
295
}
290
296
291
297
@ Override
0 commit comments