From 15b2fb1210d648ad75adb253dccfdcabdd1e554b Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 16 Apr 2019 15:59:28 -0400 Subject: [PATCH] Polish Replacing a couple of calls to Mono.fromCallable with Mono.just which seems to work with doOnDiscard except when nested inside Flux.defer. --- .../http/codec/EncoderHttpMessageWriter.java | 8 +++----- .../http/codec/xml/Jaxb2XmlEncoder.java | 12 ++++++------ .../ModelAttributeMethodArgumentResolver.java | 3 +-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java index e8058691db7..836fd3164be 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java @@ -125,16 +125,14 @@ public class EncoderHttpMessageWriter implements HttpMessageWriter { })) .flatMap(buffer -> { headers.setContentLength(buffer.readableByteCount()); - return message.writeWith( - Mono.fromCallable(() -> buffer) - .doOnDiscard(PooledDataBuffer.class, PooledDataBuffer::release)); + return message.writeWith(Mono.just(buffer) + .doOnDiscard(PooledDataBuffer.class, PooledDataBuffer::release)); }); } if (isStreamingMediaType(contentType)) { return message.writeAndFlushWith(body.map(buffer -> - Mono.fromCallable(() -> buffer) - .doOnDiscard(PooledDataBuffer.class, PooledDataBuffer::release))); + Mono.just(buffer).doOnDiscard(PooledDataBuffer.class, PooledDataBuffer::release))); } return message.writeWith(body); diff --git a/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java b/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java index 59a11970add..8109141e774 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java @@ -108,7 +108,7 @@ public class Jaxb2XmlEncoder extends AbstractSingleValueEncoder { }); } - return Flux.defer(() -> { + return Mono.fromCallable(() -> { boolean release = true; DataBuffer buffer = bufferFactory.allocateBuffer(1024); try { @@ -117,21 +117,21 @@ public class Jaxb2XmlEncoder extends AbstractSingleValueEncoder { Marshaller marshaller = initMarshaller(clazz); marshaller.marshal(value, outputStream); release = false; - return Mono.fromCallable(() -> buffer); // relying on doOnDiscard in base class + return buffer; // relying on doOnDiscard in base class } catch (MarshalException ex) { - return Flux.error(new EncodingException( - "Could not marshal " + value.getClass() + " to XML", ex)); + throw new EncodingException( + "Could not marshal " + value.getClass() + " to XML", ex); } catch (JAXBException ex) { - return Flux.error(new CodecException("Invalid JAXB configuration", ex)); + throw new CodecException("Invalid JAXB configuration", ex); } finally { if (release) { DataBufferUtils.release(buffer); } } - }); + }).flux(); } private Marshaller initMarshaller(Class clazz) throws JAXBException { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java index 32ff9c16e9c..d05e55a3d2c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java @@ -135,8 +135,7 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR BindingResult errors = binder.getBindingResult(); if (adapter != null) { return adapter.fromPublisher(errors.hasErrors() ? - Mono.error(new WebExchangeBindException(parameter, errors)) : - valueMono); + Mono.error(new WebExchangeBindException(parameter, errors)) : valueMono); } else { if (errors.hasErrors() && !hasErrorsArgument(parameter)) {