org.springframework.web.server.WebExceptionHandler Java Examples

The following examples show how to use org.springframework.web.server.WebExceptionHandler. 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: ExceptionHandlingWebHandler.java    From java-technology-stack with 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
Source File: ExceptionHandlingWebHandler.java    From spring-analysis-note with 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 #3
Source File: DefaultHandlerStrategiesBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 File: DispatcherHandlerErrorTests.java    From java-technology-stack with 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 File: WebHttpHandlerBuilder.java    From java-technology-stack with 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 #6
Source File: DefaultHandlerStrategiesBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 #7
Source File: ReactiveWebServerInitializer.java    From spring-fu with 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 #8
Source File: WebHttpHandlerBuilder.java    From spring-analysis-note with 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 #9
Source File: ExceptionHandlingWebHandler.java    From spring-analysis-note with 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 #10
Source File: DispatcherHandlerErrorTests.java    From spring-analysis-note with 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 #11
Source File: WebConfig.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@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
Source File: FibonacciConfigurer.java    From Hands-On-Reactive-Programming-with-Reactor with MIT License 4 votes vote down vote up
@Bean
WebExceptionHandler exceptionHandler(){
    return new ResponseStatusExceptionHandler();
}
 
Example #13
Source File: FibonacciConfigurer.java    From Hands-On-Reactive-Programming-with-Reactor with MIT License 4 votes vote down vote up
@Bean
WebExceptionHandler exceptionHandler(){
    return new ResponseStatusExceptionHandler();
}
 
Example #14
Source File: WebHttpHandlerBuilderTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Bean
@Order(1)
public WebExceptionHandler exceptionHandlerB() {
	return (exchange, ex) -> writeToResponse(exchange, "ExceptionHandlerB");
}
 
Example #15
Source File: WebHttpHandlerBuilderTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Bean
@Order(2)
public WebExceptionHandler exceptionHandlerA() {
	return (exchange, ex) -> writeToResponse(exchange, "ExceptionHandlerA");
}
 
Example #16
Source File: ExceptionHandlingWebHandlerTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private WebHandler createWebHandler(WebExceptionHandler... handlers) {
	return new ExceptionHandlingWebHandler(this.targetHandler, Arrays.asList(handlers));
}
 
Example #17
Source File: WebHttpHandlerBuilder.java    From java-technology-stack with 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 #18
Source File: FibonacciConfigurer.java    From Hands-On-Reactive-Programming-with-Reactor with MIT License 4 votes vote down vote up
@Bean
WebExceptionHandler exceptionHandler(){
    return new ResponseStatusExceptionHandler();
}
 
Example #19
Source File: FunctionEndpointInitializer.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
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());
}
 
Example #20
Source File: ExceptionHandlingWebHandler.java    From java-technology-stack with 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 #21
Source File: ExceptionHandlingWebHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
public ExceptionHandlingWebHandler(WebHandler delegate, List<WebExceptionHandler> handlers) {
	super(delegate);
	this.exceptionHandlers = Collections.unmodifiableList(new ArrayList<>(handlers));
}
 
Example #22
Source File: WebFluxConfigurationSupport.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Bean
@Order(0)
public WebExceptionHandler responseStatusExceptionHandler() {
	return new WebFluxResponseStatusExceptionHandler();
}
 
Example #23
Source File: DefaultHandlerStrategiesBuilder.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public List<WebExceptionHandler> exceptionHandlers() {
	return this.exceptionHandlers;
}
 
Example #24
Source File: WebHttpHandlerBuilderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Bean
@Order(1)
public WebExceptionHandler exceptionHandlerB() {
	return (exchange, ex) -> writeToResponse(exchange, "ExceptionHandlerB");
}
 
Example #25
Source File: WebHttpHandlerBuilderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Bean
@Order(2)
public WebExceptionHandler exceptionHandlerA() {
	return (exchange, ex) -> writeToResponse(exchange, "ExceptionHandlerA");
}
 
Example #26
Source File: ExceptionHandlingWebHandlerTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private WebHandler createWebHandler(WebExceptionHandler... handlers) {
	return new ExceptionHandlingWebHandler(this.targetHandler, Arrays.asList(handlers));
}
 
Example #27
Source File: WebHttpHandlerBuilder.java    From spring-analysis-note with 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 #28
Source File: ExceptionHandlingWebHandler.java    From spring-analysis-note with 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 #29
Source File: WebFluxConfigurationSupport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Bean
@Order(0)
public WebExceptionHandler responseStatusExceptionHandler() {
	return new WebFluxResponseStatusExceptionHandler();
}
 
Example #30
Source File: DefaultHandlerStrategiesBuilder.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public List<WebExceptionHandler> exceptionHandlers() {
	return this.exceptionHandlers;
}