Java Code Examples for org.springframework.web.server.WebExceptionHandler

The following examples show how to use org.springframework.web.server.WebExceptionHandler. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: ExceptionHandlingWebHandler.java    License: MIT License 6 votes vote down vote up
@Override
public Mono<Void> handle(ServerWebExchange exchange) {

	Mono<Void> completion;
	try {
		completion = super.handle(exchange);
	}
	catch (Throwable ex) {
		completion = Mono.error(ex);
	}

	for (WebExceptionHandler handler : this.exceptionHandlers) {
		completion = completion.onErrorResume(ex -> handler.handle(exchange, ex));
	}

	return completion;
}
 
Example 2
@Override
public Mono<Void> handle(ServerWebExchange exchange) {

	Mono<Void> completion;
	try {
		completion = super.handle(exchange);
	}
	catch (Throwable ex) {
		completion = Mono.error(ex);
	}

	for (WebExceptionHandler handler : this.exceptionHandlers) {
		completion = completion.onErrorResume(ex -> handler.handle(exchange, ex));
	}

	return completion;
}
 
Example 3
public DefaultHandlerStrategies(
		List<HttpMessageReader<?>> messageReaders,
		List<HttpMessageWriter<?>> messageWriters,
		List<ViewResolver> viewResolvers,
		List<WebFilter> webFilters,
		List<WebExceptionHandler> exceptionHandlers,
		LocaleContextResolver localeContextResolver) {

	this.messageReaders = unmodifiableCopy(messageReaders);
	this.messageWriters = unmodifiableCopy(messageWriters);
	this.viewResolvers = unmodifiableCopy(viewResolvers);
	this.webFilters = unmodifiableCopy(webFilters);
	this.exceptionHandlers = unmodifiableCopy(exceptionHandlers);
	this.localeContextResolver = localeContextResolver;
}
 
Example 4
Source Project: spring-analysis-note   Source File: DispatcherHandlerErrorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void webExceptionHandler() {
	ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/unknown-argument-type"));

	List<WebExceptionHandler> handlers = Collections.singletonList(new ServerError500ExceptionHandler());
	WebHandler webHandler = new ExceptionHandlingWebHandler(this.dispatcherHandler, handlers);
	webHandler.handle(exchange).block(Duration.ofSeconds(5));

	assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exchange.getResponse().getStatusCode());
}
 
Example 5
Source Project: spring-analysis-note   Source File: ExceptionHandlingWebHandler.java    License: MIT License 5 votes vote down vote up
public ExceptionHandlingWebHandler(WebHandler delegate, List<WebExceptionHandler> handlers) {
	super(delegate);
	List<WebExceptionHandler> handlersToUse = new ArrayList<>();
	handlersToUse.add(new CheckpointInsertingHandler());
	handlersToUse.addAll(handlers);
	this.exceptionHandlers = Collections.unmodifiableList(handlersToUse);
}
 
Example 6
Source Project: spring-analysis-note   Source File: WebHttpHandlerBuilder.java    License: MIT License 5 votes vote down vote up
/**
 * Add the given exception handler(s).
 * @param handlers the exception handler(s)
 */
public WebHttpHandlerBuilder exceptionHandler(WebExceptionHandler... handlers) {
	if (!ObjectUtils.isEmpty(handlers)) {
		this.exceptionHandlers.addAll(Arrays.asList(handlers));
	}
	return this;
}
 
Example 7
public DefaultHandlerStrategies(
		List<HttpMessageReader<?>> messageReaders,
		List<HttpMessageWriter<?>> messageWriters,
		List<ViewResolver> viewResolvers,
		List<WebFilter> webFilters,
		List<WebExceptionHandler> exceptionHandlers,
		LocaleContextResolver localeContextResolver) {

	this.messageReaders = unmodifiableCopy(messageReaders);
	this.messageWriters = unmodifiableCopy(messageWriters);
	this.viewResolvers = unmodifiableCopy(viewResolvers);
	this.webFilters = unmodifiableCopy(webFilters);
	this.exceptionHandlers = unmodifiableCopy(exceptionHandlers);
	this.localeContextResolver = localeContextResolver;
}
 
Example 8
@Test
public void webExceptionHandler() {
	ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/unknown-argument-type"));

	List<WebExceptionHandler> handlers = Collections.singletonList(new ServerError500ExceptionHandler());
	WebHandler webHandler = new ExceptionHandlingWebHandler(this.dispatcherHandler, handlers);
	webHandler.handle(exchange).block(Duration.ofSeconds(5));

	assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exchange.getResponse().getStatusCode());
}
 
Example 9
Source Project: java-technology-stack   Source File: WebHttpHandlerBuilder.java    License: MIT License 5 votes vote down vote up
/**
 * Add the given exception handler(s).
 * @param handlers the exception handler(s)
 */
public WebHttpHandlerBuilder exceptionHandler(WebExceptionHandler... handlers) {
	if (!ObjectUtils.isEmpty(handlers)) {
		this.exceptionHandlers.addAll(Arrays.asList(handlers));
	}
	return this;
}
 
Example 10
Source Project: spring-fu   Source File: ReactiveWebServerInitializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean("webServerFactoryCustomizerBeanPostProcessor", WebServerFactoryCustomizerBeanPostProcessor.class, WebServerFactoryCustomizerBeanPostProcessor::new);

	context.registerBean(ReactiveWebServerFactoryCustomizer.class, () -> new ReactiveWebServerFactoryCustomizer(this.serverProperties));
	context.registerBean(ConfigurableReactiveWebServerFactory.class, () -> serverFactory);
	//noinspection deprecation
	context.registerBean(ErrorAttributes.class, () -> new DefaultErrorAttributes(serverProperties.getError().isIncludeException()));
	context.registerBean(ErrorWebExceptionHandler.class,  () -> {
		ErrorWebFluxAutoConfiguration errorConfiguration = new ErrorWebFluxAutoConfiguration(this.serverProperties);
		return errorConfiguration.errorWebExceptionHandler(context.getBean(ErrorAttributes.class), this.resourceProperties, context.getBeanProvider(ViewResolver.class), context.getBean(SERVER_CODEC_CONFIGURER_BEAN_NAME, ServerCodecConfigurer.class), context);
	});
	context.registerBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class, () -> new EnableWebFluxConfigurationWrapper(context, webFluxProperties));
	context.registerBean(LOCALE_CONTEXT_RESOLVER_BEAN_NAME, LocaleContextResolver.class, () -> context.getBean(EnableWebFluxConfigurationWrapper.class).localeContextResolver());
	context.registerBean("responseStatusExceptionHandler", WebExceptionHandler.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).responseStatusExceptionHandler());

	context.registerBean(RouterFunctionMapping.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).routerFunctionMapping(context.getBean(SERVER_CODEC_CONFIGURER_BEAN_NAME, ServerCodecConfigurer.class)));
	context.registerBean(SERVER_CODEC_CONFIGURER_BEAN_NAME, ServerCodecConfigurer.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).serverCodecConfigurer());
	context.registerBean("webFluxAdapterRegistry", ReactiveAdapterRegistry.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).webFluxAdapterRegistry());
	context.registerBean("handlerFunctionAdapter", HandlerFunctionAdapter.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).handlerFunctionAdapter());
	context.registerBean("webFluxContentTypeResolver", RequestedContentTypeResolver.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).webFluxContentTypeResolver());
	context.registerBean("webFluxConversionService", FormattingConversionService.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).webFluxConversionService());
	context.registerBean("serverResponseResultHandler", ServerResponseResultHandler.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).serverResponseResultHandler(context.getBean(SERVER_CODEC_CONFIGURER_BEAN_NAME, ServerCodecConfigurer.class)));
	context.registerBean("simpleHandlerAdapter", SimpleHandlerAdapter.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).simpleHandlerAdapter());
	context.registerBean("viewResolutionResultHandler", ViewResolutionResultHandler.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).viewResolutionResultHandler(context.getBean("webFluxAdapterRegistry", ReactiveAdapterRegistry.class), context.getBean("webFluxContentTypeResolver", RequestedContentTypeResolver.class)));
	context.registerBean("webFluxValidator", Validator.class, () -> context.getBean("fuWebFluxConfiguration", EnableWebFluxConfigurationWrapper.class).webFluxValidator());
	context.registerBean(HttpHandler.class, () -> applicationContext(context).build());
	context.registerBean(WEB_HANDLER_BEAN_NAME, DispatcherHandler.class, (Supplier<DispatcherHandler>) DispatcherHandler::new);
	context.registerBean(WebFluxConfig.class, () -> new WebFluxConfig(resourceProperties, webFluxProperties, context, context.getBeanProvider(HandlerMethodArgumentResolver.class), context.getBeanProvider(CodecCustomizer.class),
		context.getBeanProvider(ResourceHandlerRegistrationCustomizer.class), context.getBeanProvider(ViewResolver.class)));
}
 
Example 11
@Bean
public WebExceptionHandler exceptionHandler() {
    return (ServerWebExchange exchange, Throwable ex) -> {
        if (ex instanceof PostNotFoundException) {
            exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
            return exchange.getResponse().setComplete();
        }
        return Mono.error(ex);
    };
}
 
Example 12
@Override
public List<WebExceptionHandler> exceptionHandlers() {
	return this.exceptionHandlers;
}
 
Example 13
Source Project: spring-analysis-note   Source File: WebFluxConfigurationSupport.java    License: MIT License 4 votes vote down vote up
@Bean
@Order(0)
public WebExceptionHandler responseStatusExceptionHandler() {
	return new WebFluxResponseStatusExceptionHandler();
}
 
Example 14
Source Project: spring-analysis-note   Source File: ExceptionHandlingWebHandler.java    License: MIT License 4 votes vote down vote up
/**
 * Return a read-only list of the configured exception handlers.
 */
public List<WebExceptionHandler> getExceptionHandlers() {
	return this.exceptionHandlers;
}
 
Example 15
Source Project: spring-analysis-note   Source File: WebHttpHandlerBuilder.java    License: MIT License 4 votes vote down vote up
/**
 * Manipulate the "live" list of currently configured exception handlers.
 * @param consumer the consumer to use
 */
public WebHttpHandlerBuilder exceptionHandlers(Consumer<List<WebExceptionHandler>> consumer) {
	consumer.accept(this.exceptionHandlers);
	return this;
}
 
Example 16
private WebHandler createWebHandler(WebExceptionHandler... handlers) {
	return new ExceptionHandlingWebHandler(this.targetHandler, Arrays.asList(handlers));
}
 
Example 17
Source Project: spring-analysis-note   Source File: WebHttpHandlerBuilderTests.java    License: MIT License 4 votes vote down vote up
@Bean
@Order(2)
public WebExceptionHandler exceptionHandlerA() {
	return (exchange, ex) -> writeToResponse(exchange, "ExceptionHandlerA");
}
 
Example 18
Source Project: spring-analysis-note   Source File: WebHttpHandlerBuilderTests.java    License: MIT License 4 votes vote down vote up
@Bean
@Order(1)
public WebExceptionHandler exceptionHandlerB() {
	return (exchange, ex) -> writeToResponse(exchange, "ExceptionHandlerB");
}
 
Example 19
@Override
public List<WebExceptionHandler> exceptionHandlers() {
	return this.exceptionHandlers;
}
 
Example 20
@Bean
@Order(0)
public WebExceptionHandler responseStatusExceptionHandler() {
	return new WebFluxResponseStatusExceptionHandler();
}
 
Example 21
public ExceptionHandlingWebHandler(WebHandler delegate, List<WebExceptionHandler> handlers) {
	super(delegate);
	this.exceptionHandlers = Collections.unmodifiableList(new ArrayList<>(handlers));
}
 
Example 22
/**
 * Return a read-only list of the configured exception handlers.
 */
public List<WebExceptionHandler> getExceptionHandlers() {
	return this.exceptionHandlers;
}
 
Example 23
Source Project: java-technology-stack   Source File: WebHttpHandlerBuilder.java    License: MIT License 4 votes vote down vote up
/**
 * Manipulate the "live" list of currently configured exception handlers.
 * @param consumer the consumer to use
 */
public WebHttpHandlerBuilder exceptionHandlers(Consumer<List<WebExceptionHandler>> consumer) {
	consumer.accept(this.exceptionHandlers);
	return this;
}
 
Example 24
private WebHandler createWebHandler(WebExceptionHandler... handlers) {
	return new ExceptionHandlingWebHandler(this.targetHandler, Arrays.asList(handlers));
}
 
Example 25
Source Project: java-technology-stack   Source File: WebHttpHandlerBuilderTests.java    License: MIT License 4 votes vote down vote up
@Bean
@Order(2)
public WebExceptionHandler exceptionHandlerA() {
	return (exchange, ex) -> writeToResponse(exchange, "ExceptionHandlerA");
}
 
Example 26
Source Project: java-technology-stack   Source File: WebHttpHandlerBuilderTests.java    License: MIT License 4 votes vote down vote up
@Bean
@Order(1)
public WebExceptionHandler exceptionHandlerB() {
	return (exchange, ex) -> writeToResponse(exchange, "ExceptionHandlerB");
}
 
Example 27
@Bean
WebExceptionHandler exceptionHandler(){
    return new ResponseStatusExceptionHandler();
}
 
Example 28
@Bean
WebExceptionHandler exceptionHandler(){
    return new ResponseStatusExceptionHandler();
}
 
Example 29
@Bean
WebExceptionHandler exceptionHandler(){
    return new ResponseStatusExceptionHandler();
}
 
Example 30
private HttpWebHandlerAdapter httpHandler(GenericApplicationContext context) {
	return (HttpWebHandlerAdapter) RouterFunctions.toHttpHandler(context.getBean(RouterFunction.class),
			HandlerStrategies.empty().exceptionHandler(context.getBean(WebExceptionHandler.class))
					.codecs(config -> config.registerDefaults(true)).build());
}