Java Code Examples for org.springframework.web.reactive.HandlerResult#getReturnValue()

The following examples show how to use org.springframework.web.reactive.HandlerResult#getReturnValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestMappingInfoHandlerMappingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void testHttpOptions(String requestURI, Set<HttpMethod> allowedMethods) {
	ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options(requestURI));
	HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();

	BindingContext bindingContext = new BindingContext();
	InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod);
	Mono<HandlerResult> mono = invocable.invoke(exchange, bindingContext);

	HandlerResult result = mono.block();
	assertNotNull(result);

	Object value = result.getReturnValue();
	assertNotNull(value);
	assertEquals(HttpHeaders.class, value.getClass());
	assertEquals(allowedMethods, ((HttpHeaders) value).getAllow());
}
 
Example 2
Source File: RequestMappingInfoHandlerMappingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void testHttpOptions(String requestURI, Set<HttpMethod> allowedMethods) {
	ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options(requestURI));
	HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();

	BindingContext bindingContext = new BindingContext();
	InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod);
	Mono<HandlerResult> mono = invocable.invoke(exchange, bindingContext);

	HandlerResult result = mono.block();
	assertNotNull(result);

	Object value = result.getReturnValue();
	assertNotNull(value);
	assertEquals(HttpHeaders.class, value.getClass());
	assertEquals(allowedMethods, ((HttpHeaders) value).getAllow());
}
 
Example 3
Source File: ServerResponseResultHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
	ServerResponse response = (ServerResponse) result.getReturnValue();
	Assert.state(response != null, "No ServerResponse");
	return response.writeTo(exchange, new ServerResponse.Context() {
		@Override
		public List<HttpMessageWriter<?>> messageWriters() {
			return messageWriters;
		}
		@Override
		public List<ViewResolver> viewResolvers() {
			return viewResolvers;
		}
	});
}
 
Example 4
Source File: ModelInitializer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Mono<Void> handleResult(HandlerResult handlerResult, BindingContext bindingContext) {
	Object value = handlerResult.getReturnValue();
	if (value != null) {
		ResolvableType type = handlerResult.getReturnType();
		ReactiveAdapter adapter = this.adapterRegistry.getAdapter(type.resolve(), value);
		if (isAsyncVoidType(type, adapter)) {
			return Mono.from(adapter.toPublisher(value));
		}
		String name = getAttributeName(handlerResult.getReturnTypeSource());
		bindingContext.getModel().asMap().putIfAbsent(name, value);
	}
	return Mono.empty();
}
 
Example 5
Source File: InitBinderBindingContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void invokeBinderMethod(
		WebExchangeDataBinder dataBinder, ServerWebExchange exchange, SyncInvocableHandlerMethod binderMethod) {

	HandlerResult result = binderMethod.invokeForHandlerResult(exchange, this.binderMethodContext, dataBinder);
	if (result != null && result.getReturnValue() != null) {
		throw new IllegalStateException(
				"@InitBinder methods must not return a value (should be void): " + binderMethod);
	}
	// Should not happen (no Model argument resolution) ...
	if (!this.binderMethodContext.getModel().asMap().isEmpty()) {
		throw new IllegalStateException(
				"@InitBinder methods are not allowed to add model attributes: " + binderMethod);
	}
}
 
Example 6
Source File: ResponseEntityResultHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
private static Class<?> resolveReturnValueType(HandlerResult result) {
	Class<?> valueType = result.getReturnType().toClass();
	Object value = result.getReturnValue();
	if (valueType == Object.class && value != null) {
		valueType = value.getClass();
	}
	return valueType;
}
 
Example 7
Source File: ServerResponseResultHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
	ServerResponse response = (ServerResponse) result.getReturnValue();
	Assert.state(response != null, "No ServerResponse");
	return response.writeTo(exchange, new ServerResponse.Context() {
		@Override
		public List<HttpMessageWriter<?>> messageWriters() {
			return messageWriters;
		}
		@Override
		public List<ViewResolver> viewResolvers() {
			return viewResolvers;
		}
	});
}
 
Example 8
Source File: ModelInitializer.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Mono<Void> handleResult(HandlerResult handlerResult, BindingContext bindingContext) {
	Object value = handlerResult.getReturnValue();
	if (value != null) {
		ResolvableType type = handlerResult.getReturnType();
		ReactiveAdapter adapter = this.adapterRegistry.getAdapter(type.resolve(), value);
		if (isAsyncVoidType(type, adapter)) {
			return Mono.from(adapter.toPublisher(value));
		}
		String name = getAttributeName(handlerResult.getReturnTypeSource());
		bindingContext.getModel().asMap().putIfAbsent(name, value);
	}
	return Mono.empty();
}
 
Example 9
Source File: InitBinderBindingContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void invokeBinderMethod(
		WebExchangeDataBinder dataBinder, ServerWebExchange exchange, SyncInvocableHandlerMethod binderMethod) {

	HandlerResult result = binderMethod.invokeForHandlerResult(exchange, this.binderMethodContext, dataBinder);
	if (result != null && result.getReturnValue() != null) {
		throw new IllegalStateException(
				"@InitBinder methods must not return a value (should be void): " + binderMethod);
	}
	// Should not happen (no Model argument resolution) ...
	if (!this.binderMethodContext.getModel().asMap().isEmpty()) {
		throw new IllegalStateException(
				"@InitBinder methods are not allowed to add model attributes: " + binderMethod);
	}
}
 
Example 10
Source File: ResponseEntityResultHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
private static Class<?> resolveReturnValueType(HandlerResult result) {
	Class<?> valueType = result.getReturnType().toClass();
	Object value = result.getReturnValue();
	if (valueType == Object.class && value != null) {
		valueType = value.getClass();
	}
	return valueType;
}
 
Example 11
Source File: ServerResponseResultHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean supports(HandlerResult result) {
	return (result.getReturnValue() instanceof ServerResponse);
}
 
Example 12
Source File: ViewResolutionResultHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
	Mono<Object> valueMono;
	ResolvableType valueType;
	ReactiveAdapter adapter = getAdapter(result);

	if (adapter != null) {
		if (adapter.isMultiValue()) {
			throw new IllegalArgumentException(
					"Multi-value reactive types not supported in view resolution: " + result.getReturnType());
		}

		valueMono = (result.getReturnValue() != null ?
				Mono.from(adapter.toPublisher(result.getReturnValue())) : Mono.empty());

		valueType = (adapter.isNoValue() ? ResolvableType.forClass(Void.class) :
				result.getReturnType().getGeneric());
	}
	else {
		valueMono = Mono.justOrEmpty(result.getReturnValue());
		valueType = result.getReturnType();
	}

	return valueMono
			.switchIfEmpty(exchange.isNotModified() ? Mono.empty() : NO_VALUE_MONO)
			.flatMap(returnValue -> {

				Mono<List<View>> viewsMono;
				Model model = result.getModel();
				MethodParameter parameter = result.getReturnTypeSource();
				Locale locale = LocaleContextHolder.getLocale(exchange.getLocaleContext());

				Class<?> clazz = valueType.toClass();
				if (clazz == Object.class) {
					clazz = returnValue.getClass();
				}

				if (returnValue == NO_VALUE || clazz == void.class || clazz == Void.class) {
					viewsMono = resolveViews(getDefaultViewName(exchange), locale);
				}
				else if (CharSequence.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {
					viewsMono = resolveViews(returnValue.toString(), locale);
				}
				else if (Rendering.class.isAssignableFrom(clazz)) {
					Rendering render = (Rendering) returnValue;
					HttpStatus status = render.status();
					if (status != null) {
						exchange.getResponse().setStatusCode(status);
					}
					exchange.getResponse().getHeaders().putAll(render.headers());
					model.addAllAttributes(render.modelAttributes());
					Object view = render.view();
					if (view == null) {
						view = getDefaultViewName(exchange);
					}
					viewsMono = (view instanceof String ? resolveViews((String) view, locale) :
							Mono.just(Collections.singletonList((View) view)));
				}
				else if (Model.class.isAssignableFrom(clazz)) {
					model.addAllAttributes(((Model) returnValue).asMap());
					viewsMono = resolveViews(getDefaultViewName(exchange), locale);
				}
				else if (Map.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {
					model.addAllAttributes((Map<String, ?>) returnValue);
					viewsMono = resolveViews(getDefaultViewName(exchange), locale);
				}
				else if (View.class.isAssignableFrom(clazz)) {
					viewsMono = Mono.just(Collections.singletonList((View) returnValue));
				}
				else {
					String name = getNameForReturnValue(parameter);
					model.addAttribute(name, returnValue);
					viewsMono = resolveViews(getDefaultViewName(exchange), locale);
				}

				updateBindingContext(result.getBindingContext(), exchange);

				return viewsMono.flatMap(views -> render(views, model.asMap(), exchange));
			});
}
 
Example 13
Source File: ResponseBodyResultHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
	Object body = result.getReturnValue();
	MethodParameter bodyTypeParameter = result.getReturnTypeSource();
	return writeBody(body, bodyTypeParameter, exchange);
}
 
Example 14
Source File: ServerResponseResultHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean supports(HandlerResult result) {
	return (result.getReturnValue() instanceof ServerResponse);
}
 
Example 15
Source File: ViewResolutionResultHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
	Mono<Object> valueMono;
	ResolvableType valueType;
	ReactiveAdapter adapter = getAdapter(result);

	if (adapter != null) {
		if (adapter.isMultiValue()) {
			throw new IllegalArgumentException(
					"Multi-value reactive types not supported in view resolution: " + result.getReturnType());
		}

		valueMono = (result.getReturnValue() != null ?
				Mono.from(adapter.toPublisher(result.getReturnValue())) : Mono.empty());

		valueType = (adapter.isNoValue() ? ResolvableType.forClass(Void.class) :
				result.getReturnType().getGeneric());
	}
	else {
		valueMono = Mono.justOrEmpty(result.getReturnValue());
		valueType = result.getReturnType();
	}

	return valueMono
			.switchIfEmpty(exchange.isNotModified() ? Mono.empty() : NO_VALUE_MONO)
			.flatMap(returnValue -> {

				Mono<List<View>> viewsMono;
				Model model = result.getModel();
				MethodParameter parameter = result.getReturnTypeSource();
				Locale locale = LocaleContextHolder.getLocale(exchange.getLocaleContext());

				Class<?> clazz = valueType.toClass();
				if (clazz == Object.class) {
					clazz = returnValue.getClass();
				}

				if (returnValue == NO_VALUE || clazz == void.class || clazz == Void.class) {
					viewsMono = resolveViews(getDefaultViewName(exchange), locale);
				}
				else if (CharSequence.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {
					viewsMono = resolveViews(returnValue.toString(), locale);
				}
				else if (Rendering.class.isAssignableFrom(clazz)) {
					Rendering render = (Rendering) returnValue;
					HttpStatus status = render.status();
					if (status != null) {
						exchange.getResponse().setStatusCode(status);
					}
					exchange.getResponse().getHeaders().putAll(render.headers());
					model.addAllAttributes(render.modelAttributes());
					Object view = render.view();
					if (view == null) {
						view = getDefaultViewName(exchange);
					}
					viewsMono = (view instanceof String ? resolveViews((String) view, locale) :
							Mono.just(Collections.singletonList((View) view)));
				}
				else if (Model.class.isAssignableFrom(clazz)) {
					model.addAllAttributes(((Model) returnValue).asMap());
					viewsMono = resolveViews(getDefaultViewName(exchange), locale);
				}
				else if (Map.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {
					model.addAllAttributes((Map<String, ?>) returnValue);
					viewsMono = resolveViews(getDefaultViewName(exchange), locale);
				}
				else if (View.class.isAssignableFrom(clazz)) {
					viewsMono = Mono.just(Collections.singletonList((View) returnValue));
				}
				else {
					String name = getNameForReturnValue(parameter);
					model.addAttribute(name, returnValue);
					viewsMono = resolveViews(getDefaultViewName(exchange), locale);
				}

				updateBindingContext(result.getBindingContext(), exchange);

				return viewsMono.flatMap(views -> render(views, model.asMap(), exchange));
			});
}
 
Example 16
Source File: ResponseBodyResultHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
	Object body = result.getReturnValue();
	MethodParameter bodyTypeParameter = result.getReturnTypeSource();
	return writeBody(body, bodyTypeParameter, exchange);
}