Class ProtobufResponseConverterFunction
java.lang.Object
com.linecorp.armeria.server.protobuf.ProtobufResponseConverterFunction
- All Implemented Interfaces:
ResponseConverterFunction
@UnstableApi
public final class ProtobufResponseConverterFunction
extends Object
implements ResponseConverterFunction
A
ResponseConverterFunction
which creates an HttpResponse
with
content-type: application/protobuf
or content-type: application/json; charset=utf-8
.
If the returned object is an instance of MessageLite
, the object can be converted to either
Protocol Buffers or
JSON format.
Conversion of multiple Protobuf messages
A sequence of Protocol Buffer messages can not be handled by thisResponseConverterFunction
,
because Protocol Buffers wire format is not self-delimiting.
See
Streaming Multiple Messages
for more information.
However, Publisher
, Stream
and Iterable
are supported when converting to
JSON array.
JavaScript Object Notation (JSON) Text Sequences
is also supported for Publisher
, Stream
.
Note that this ResponseConverterFunction
is applied to an annotated service by default,
so you don't have to specify this converter explicitly unless you want to use your own JsonFormat.Printer
.
The JsonFormat.printer()
is used by default to format the response content.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an instance with the defaultJsonFormat.Printer
.ProtobufResponseConverterFunction
(JsonFormat.Printer jsonPrinter) Creates an instance with the specifiedJsonFormat.Printer
. -
Method Summary
Modifier and TypeMethodDescriptionconvertResponse
(ServiceRequestContext ctx, ResponseHeaders headers, @Nullable Object result, HttpHeaders trailers) ReturnsHttpResponse
instance corresponds to the givenresult
.isResponseStreaming
(Type returnType, @Nullable MediaType produceType) Returns whether anHttpResponse
of an annotated service should be streamed.
-
Constructor Details
-
ProtobufResponseConverterFunction
public ProtobufResponseConverterFunction()Creates an instance with the defaultJsonFormat.Printer
. -
ProtobufResponseConverterFunction
Creates an instance with the specifiedJsonFormat.Printer
.
-
-
Method Details
-
isResponseStreaming
Description copied from interface:ResponseConverterFunction
Returns whether anHttpResponse
of an annotated service should be streamed.null
if this converter cannot convert theresponseType
to anHttpResponse
.true
can be returned if the response is a streaming type, or theResponseConverterFunction
has not been optimized for theresponseType
through overriding this method for backward compatibility.This method is used as a performance optimization hint. If the
returnType
andproduceType
are not a streaming response, it is recommended to returnfalse
for the better performance.Note that you should never return
false
for a streaming response. The non-streaming response is aggregated before being sent.- Specified by:
isResponseStreaming
in interfaceResponseConverterFunction
- Parameters:
returnType
- the return type of the annotated service.produceType
- the negotiated producible media type of the annotated service.null
if the media type negotiation is not used for the service.
-
convertResponse
public HttpResponse convertResponse(ServiceRequestContext ctx, ResponseHeaders headers, @Nullable @Nullable Object result, HttpHeaders trailers) throws Exception Description copied from interface:ResponseConverterFunction
ReturnsHttpResponse
instance corresponds to the givenresult
. CallsResponseConverterFunction.fallthrough()
if this converter cannot convert theresult
to theHttpResponse
.- Specified by:
convertResponse
in interfaceResponseConverterFunction
- Parameters:
headers
- The HTTP headers that you might want to use to create theHttpResponse
. The status of headers isHttpStatus.OK
by default orHttpStatus.NO_CONTENT
if the annotated method returnsvoid
, unless you specify it withStatusCode
on the method. The headers also will include aMediaType
ifServiceRequestContext.negotiatedResponseMediaType()
returns it. If the annotated method returns anHttpResult
or aResponseEntity
, the headers provided by them will be given as they are. Please note that the additional headers set byServiceRequestContext.mutateAdditionalResponseHeaders(Consumer)
andAdditionalHeader
are not included in this headers.result
- The result of the service method.trailers
- The HTTP trailers that you might want to use to create theHttpResponse
. If the annotated method returns anHttpResult
or aResponseEntity
, the trailers provided by them will be given as they are. Please note that the additional trailers set byServiceRequestContext.mutateAdditionalResponseTrailers(Consumer)
andAdditionalTrailer
are not included in this trailers.- Throws:
Exception
-