Search in sources :

Example 1 with ServerHttpMessageWriter

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)));
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType) ServerHttpMessageWriter(org.springframework.http.codec.ServerHttpMessageWriter) ReactiveAdapter(org.springframework.core.ReactiveAdapter) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse)

Aggregations

ReactiveAdapter (org.springframework.core.ReactiveAdapter)1 ResolvableType (org.springframework.core.ResolvableType)1 MediaType (org.springframework.http.MediaType)1 ServerHttpMessageWriter (org.springframework.http.codec.ServerHttpMessageWriter)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1 NotAcceptableStatusException (org.springframework.web.server.NotAcceptableStatusException)1