Browse Source

Polish (minor) in AbstractMessageReaderArgumentResolver

pull/1795/head
Rossen Stoyanchev 8 years ago
parent
commit
06041ea4e2
  1. 32
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageReaderArgumentResolver.java

32
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageReaderArgumentResolver.java

@ -127,8 +127,8 @@ public abstract class AbstractMessageReaderArgumentResolver extends HandlerMetho
/** /**
* Read the body from a method argument with {@link HttpMessageReader}. * Read the body from a method argument with {@link HttpMessageReader}.
* @param bodyParameter the {@link MethodParameter} to read * @param bodyParam the {@link MethodParameter} to read
* @param actualParameter the actual {@link MethodParameter} to read; could be different * @param actualParam the actual {@link MethodParameter} to read; could be different
* from {@code bodyParameter} when processing {@code HttpEntity} for example * from {@code bodyParameter} when processing {@code HttpEntity} for example
* @param isBodyRequired true if the body is required * @param isBodyRequired true if the body is required
* @param bindingContext the binding context to use * @param bindingContext the binding context to use
@ -136,12 +136,11 @@ public abstract class AbstractMessageReaderArgumentResolver extends HandlerMetho
* @return the body * @return the body
* @since 5.0.2 * @since 5.0.2
*/ */
protected Mono<Object> readBody(MethodParameter bodyParameter, @Nullable MethodParameter actualParameter, protected Mono<Object> readBody(MethodParameter bodyParam, @Nullable MethodParameter actualParam,
boolean isBodyRequired, BindingContext bindingContext, ServerWebExchange exchange) { boolean isBodyRequired, BindingContext bindingContext, ServerWebExchange exchange) {
ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParameter); ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParam);
ResolvableType actualType = (actualParameter == null ? ResolvableType actualType = actualParam == null ? bodyType : ResolvableType.forMethodParameter(actualParam);
bodyType : ResolvableType.forMethodParameter(actualParameter));
Class<?> resolvedType = bodyType.resolve(); Class<?> resolvedType = bodyType.resolve();
ReactiveAdapter adapter = (resolvedType != null ? getAdapterRegistry().getAdapter(resolvedType) : null); ReactiveAdapter adapter = (resolvedType != null ? getAdapterRegistry().getAdapter(resolvedType) : null);
ResolvableType elementType = (adapter != null ? bodyType.getGeneric() : bodyType); ResolvableType elementType = (adapter != null ? bodyType.getGeneric() : bodyType);
@ -153,42 +152,37 @@ public abstract class AbstractMessageReaderArgumentResolver extends HandlerMetho
MediaType mediaType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM); MediaType mediaType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);
Supplier<Throwable> missingBodyError = isBodyRequired || (adapter != null && !adapter.supportsEmpty()) ? Supplier<Throwable> missingBodyError = isBodyRequired || (adapter != null && !adapter.supportsEmpty()) ?
() -> handleMissingBody(bodyParameter) : null; () -> handleMissingBody(bodyParam) : null;
for (HttpMessageReader<?> reader : getMessageReaders()) { for (HttpMessageReader<?> reader : getMessageReaders()) {
if (reader.canRead(elementType, mediaType)) { if (reader.canRead(elementType, mediaType)) {
Map<String, Object> readHints = Collections.emptyMap(); Map<String, Object> readHints = Collections.emptyMap();
if (adapter != null && adapter.isMultiValue()) { if (adapter != null && adapter.isMultiValue()) {
Flux<?> flux = reader.read(actualType, elementType, request, response, readHints); Flux<?> flux = reader.read(actualType, elementType, request, response, readHints);
flux = flux.onErrorResume(ex -> Flux.error(handleReadError(bodyParameter, ex))); flux = flux.onErrorResume(ex -> Flux.error(handleReadError(bodyParam, ex)));
if (missingBodyError != null) { if (missingBodyError != null) {
flux = flux.switchIfEmpty(Flux.error(missingBodyError)); flux = flux.switchIfEmpty(Flux.error(missingBodyError));
} }
Object[] hints = extractValidationHints(bodyParameter); Object[] hints = extractValidationHints(bodyParam);
if (hints != null) { if (hints != null) {
flux = flux.doOnNext(target -> flux = flux.doOnNext(target ->
validate(target, hints, bodyParameter, bindingContext, exchange)); validate(target, hints, bodyParam, bindingContext, exchange));
} }
return Mono.just(adapter.fromPublisher(flux)); return Mono.just(adapter.fromPublisher(flux));
} }
else { else {
// Single-value (with or without reactive type wrapper) // Single-value (with or without reactive type wrapper)
Mono<?> mono = reader.readMono(actualType, elementType, request, response, readHints); Mono<?> mono = reader.readMono(actualType, elementType, request, response, readHints);
mono = mono.onErrorResume(ex -> Mono.error(handleReadError(bodyParameter, ex))); mono = mono.onErrorResume(ex -> Mono.error(handleReadError(bodyParam, ex)));
if (missingBodyError != null) { if (missingBodyError != null) {
mono = mono.switchIfEmpty(Mono.error(missingBodyError)); mono = mono.switchIfEmpty(Mono.error(missingBodyError));
} }
Object[] hints = extractValidationHints(bodyParameter); Object[] hints = extractValidationHints(bodyParam);
if (hints != null) { if (hints != null) {
mono = mono.doOnNext(target -> mono = mono.doOnNext(target ->
validate(target, hints, bodyParameter, bindingContext, exchange)); validate(target, hints, bodyParam, bindingContext, exchange));
}
if (adapter != null) {
return Mono.just(adapter.fromPublisher(mono));
}
else {
return Mono.from(mono);
} }
return adapter != null ? Mono.just(adapter.fromPublisher(mono)) : Mono.from(mono);
} }
} }
} }

Loading…
Cancel
Save