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

The following examples show how to use org.springframework.web.reactive.HandlerResult#getReturnTypeSource() . 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: ResponseBodyResultHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean supports(HandlerResult result) {
	MethodParameter returnType = result.getReturnTypeSource();
	Class<?> containingClass = returnType.getContainingClass();
	return (AnnotatedElementUtils.hasAnnotation(containingClass, ResponseBody.class) ||
			returnType.hasMethodAnnotation(ResponseBody.class));
}
 
Example 2
Source File: ResponseBodyResultHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean supports(HandlerResult result) {
	MethodParameter returnType = result.getReturnTypeSource();
	Class<?> containingClass = returnType.getContainingClass();
	return (AnnotatedElementUtils.hasAnnotation(containingClass, ResponseBody.class) ||
			returnType.hasMethodAnnotation(ResponseBody.class));
}
 
Example 3
Source File: ResponseEntityResultHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {

	Mono<?> returnValueMono;
	MethodParameter bodyParameter;
	ReactiveAdapter adapter = getAdapter(result);
	MethodParameter actualParameter = result.getReturnTypeSource();

	if (adapter != null) {
		Assert.isTrue(!adapter.isMultiValue(), "Only a single ResponseEntity supported");
		returnValueMono = Mono.from(adapter.toPublisher(result.getReturnValue()));
		bodyParameter = actualParameter.nested().nested();
	}
	else {
		returnValueMono = Mono.justOrEmpty(result.getReturnValue());
		bodyParameter = actualParameter.nested();
	}

	return returnValueMono.flatMap(returnValue -> {
		HttpEntity<?> httpEntity;
		if (returnValue instanceof HttpEntity) {
			httpEntity = (HttpEntity<?>) returnValue;
		}
		else if (returnValue instanceof HttpHeaders) {
			httpEntity = new ResponseEntity<>((HttpHeaders) returnValue, HttpStatus.OK);
		}
		else {
			throw new IllegalArgumentException(
					"HttpEntity or HttpHeaders expected but got: " + returnValue.getClass());
		}

		if (httpEntity instanceof ResponseEntity) {
			ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
			ServerHttpResponse response = exchange.getResponse();
			if (response instanceof AbstractServerHttpResponse) {
				((AbstractServerHttpResponse) response).setStatusCodeValue(responseEntity.getStatusCodeValue());
			}
			else {
				response.setStatusCode(responseEntity.getStatusCode());
			}
		}

		HttpHeaders entityHeaders = httpEntity.getHeaders();
		HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
		if (!entityHeaders.isEmpty()) {
			entityHeaders.entrySet().stream()
					.forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
		}

		if (httpEntity.getBody() == null || returnValue instanceof HttpHeaders) {
			return exchange.getResponse().setComplete();
		}

		String etag = entityHeaders.getETag();
		Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
		HttpMethod httpMethod = exchange.getRequest().getMethod();
		if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
			return exchange.getResponse().setComplete();
		}

		return writeBody(httpEntity.getBody(), bodyParameter, actualParameter, exchange);
	});
}
 
Example 4
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 5
Source File: ResponseEntityResultHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {

	Mono<?> returnValueMono;
	MethodParameter bodyParameter;
	ReactiveAdapter adapter = getAdapter(result);
	MethodParameter actualParameter = result.getReturnTypeSource();

	if (adapter != null) {
		Assert.isTrue(!adapter.isMultiValue(), "Only a single ResponseEntity supported");
		returnValueMono = Mono.from(adapter.toPublisher(result.getReturnValue()));
		bodyParameter = actualParameter.nested().nested();
	}
	else {
		returnValueMono = Mono.justOrEmpty(result.getReturnValue());
		bodyParameter = actualParameter.nested();
	}

	return returnValueMono.flatMap(returnValue -> {
		HttpEntity<?> httpEntity;
		if (returnValue instanceof HttpEntity) {
			httpEntity = (HttpEntity<?>) returnValue;
		}
		else if (returnValue instanceof HttpHeaders) {
			httpEntity = new ResponseEntity<>((HttpHeaders) returnValue, HttpStatus.OK);
		}
		else {
			throw new IllegalArgumentException(
					"HttpEntity or HttpHeaders expected but got: " + returnValue.getClass());
		}

		if (httpEntity instanceof ResponseEntity) {
			ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
			ServerHttpResponse response = exchange.getResponse();
			if (response instanceof AbstractServerHttpResponse) {
				((AbstractServerHttpResponse) response).setStatusCodeValue(responseEntity.getStatusCodeValue());
			}
			else {
				response.setStatusCode(responseEntity.getStatusCode());
			}
		}

		HttpHeaders entityHeaders = httpEntity.getHeaders();
		HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
		if (!entityHeaders.isEmpty()) {
			entityHeaders.entrySet().stream()
					.forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
		}

		if (httpEntity.getBody() == null || returnValue instanceof HttpHeaders) {
			return exchange.getResponse().setComplete();
		}

		String etag = entityHeaders.getETag();
		Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
		HttpMethod httpMethod = exchange.getRequest().getMethod();
		if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
			return exchange.getResponse().setComplete();
		}

		return writeBody(httpEntity.getBody(), bodyParameter, actualParameter, exchange);
	});
}
 
Example 6
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);
}