Don’t say much, get up and do

SoulWebHandler is a request processing interface that can handle requests through a SoulWebHandler interface. This interface can handle requests through a SoulWebHandler interface. This interface directly inherits Spring’s WebHandler interface

/** * Contract to handle a web request. * * <p>Use {@link HttpWebHandlerAdapter} to adapt a {@code WebHandler} to an * {@link org.springframework.http.server.reactive.HttpHandler HttpHandler}. * The {@link WebHttpHandlerBuilder} provides a  convenient way to do that while * also optionally configuring one or more filters and/or exception handlers. * * @author Rossen Stoyanchev * @since 5.0 */ public Interface WebHandler {/** ** Handle the Web server exchange exchange the current server exchange * @return {@code Mono<Void>} to indicate when request handling is complete */ Mono<Void> handle(ServerWebExchange exchange); }Copy the code

This interface is new to spring5 and is used to handle web requests using reative programming. The only method of the webhandler interface is handled by an HTTP request adapter, HttpWebHandlerAdapter

@Override public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { if (this.forwardedHeaderTransformer ! = null) { request = this.forwardedHeaderTransformer.apply(request); } ServerWebExchange exchange = createExchange(request, response); LogFormatUtils.traceDebug(logger, traceOn -> exchange.getLogPrefix() + formatRequest(exchange.getRequest()) + (traceOn ? ", headers=" + formatHeaders(exchange.getRequest().getHeaders()) : "")); Return getDelegate().handle(exchange).doonSuccess (aVoid -> logResponse(exchange)).onErrorResume(ex) -> handleUnresolvedError(exchange, ex)) .then(Mono.defer(response::setComplete)); } protected ServerWebExchange createExchange(ServerHttpRequest request, ServerHttpResponse response) { return new DefaultServerWebExchange(request, response, this.sessionManager, getCodecConfigurer(), getLocaleContextResolver(), this.applicationContext); }Copy the code

The service adapter will eventually be ServletHttpHandlerAdapter method calls, and the familiar service, is our initial learning Java serlvet container must implement the service, to handle HTTP requests, This adapter is ultimately a concrete implementation of Java’s Servlet interface

	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		// Check for existing error attribute first
		if (DispatcherType.ASYNC.equals(request.getDispatcherType())) {
			Throwable ex = (Throwable) request.getAttribute(WRITE_ERROR_ATTRIBUTE_NAME);
			throw new ServletException("Failed to create response content", ex);
		}

		// Start async before Read/WriteListener registration
		AsyncContext asyncContext = request.startAsync();
		asyncContext.setTimeout(-1);

		ServletServerHttpRequest httpRequest;
		try {
			httpRequest = createRequest(((HttpServletRequest) request), asyncContext);
		}
		catch (URISyntaxException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Failed to get request  URL: " + ex.getMessage());
			}
			((HttpServletResponse) response).setStatus(400);
			asyncContext.complete();
			return;
		}

		ServerHttpResponse httpResponse = createResponse(((HttpServletResponse) response), asyncContext, httpRequest);
		if (httpRequest.getMethod() == HttpMethod.HEAD) {
			httpResponse = new HttpHeadResponseDecorator(httpResponse);
		}

		AtomicBoolean isCompleted = new AtomicBoolean();
		HandlerResultAsyncListener listener = new HandlerResultAsyncListener(isCompleted, httpRequest);
		asyncContext.addListener(listener);

		HandlerResultSubscriber subscriber = new HandlerResultSubscriber(asyncContext, isCompleted, httpRequest);
		this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
	}
Copy the code

The interface that inherits this Service method is the well-known GenericServlet general-purpose Servlet interface, which is also the indirect parent of SpringMVC’s core DispatcherServlet

Spring’s new responsive programming is compatible with the previous SpringMVC framework under the same servlet framework