use of org.springframework.http.codec.ServerHttpMessageWriter in project spring-framework by spring-projects.
the class AbstractMessageWriterResultHandler method writeBody.
@SuppressWarnings("unchecked")
protected Mono<Void> writeBody(Object body, MethodParameter bodyParameter, ServerWebExchange exchange) {
ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParameter);
Class<?> bodyClass = bodyType.resolve();
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(bodyClass, body);
Publisher<?> publisher;
ResolvableType elementType;
if (adapter != null) {
publisher = adapter.toPublisher(body);
elementType = adapter.isNoValue() ? ResolvableType.forClass(Void.class) : bodyType.getGeneric(0);
} else {
publisher = Mono.justOrEmpty(body);
elementType = (bodyClass == null && body != null ? ResolvableType.forInstance(body) : bodyType);
}
if (void.class == elementType.getRawClass() || Void.class == elementType.getRawClass()) {
return Mono.from((Publisher<Void>) publisher);
}
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
MediaType bestMediaType = selectMediaType(exchange, () -> getProducibleMediaTypes(elementType));
if (bestMediaType != null) {
for (HttpMessageWriter<?> messageWriter : getMessageWriters()) {
if (messageWriter.canWrite(elementType, bestMediaType)) {
return (messageWriter instanceof ServerHttpMessageWriter ? ((ServerHttpMessageWriter<?>) messageWriter).write((Publisher) publisher, bodyType, elementType, bestMediaType, request, response, Collections.emptyMap()) : messageWriter.write((Publisher) publisher, elementType, bestMediaType, response, Collections.emptyMap()));
}
}
} else {
if (getProducibleMediaTypes(elementType).isEmpty()) {
return Mono.error(new IllegalStateException("No converter for return value type: " + elementType));
}
}
return Mono.error(new NotAcceptableStatusException(getProducibleMediaTypes(elementType)));
}
Aggregations