com.nike.backstopper.handler.spring.webflux.config.BackstopperSpringWebFluxConfig Maven / Gradle / Ivy
Show all versions of backstopper-spring-web-flux Show documentation
package com.nike.backstopper.handler.spring.webflux.config;
import com.nike.backstopper.apierror.projectspecificinfo.ProjectApiErrors;
import com.nike.backstopper.apierror.sample.SampleProjectApiErrorsBase;
import com.nike.backstopper.handler.ApiExceptionHandlerBase;
import com.nike.backstopper.handler.spring.webflux.SpringWebfluxApiExceptionHandler;
import com.nike.backstopper.handler.spring.webflux.SpringWebfluxApiExceptionHandlerUtils;
import com.nike.backstopper.handler.spring.webflux.SpringWebfluxUnhandledExceptionHandler;
import com.nike.backstopper.handler.spring.webflux.listener.SpringWebFluxApiExceptionHandlerListenerList;
import com.nike.backstopper.service.ClientDataValidationService;
import com.nike.backstopper.service.FailFastServersideValidationService;
import com.nike.backstopper.service.NoOpJsr303Validator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import jakarta.validation.Validator;
/**
* This Spring WebFlux configuration is an alternative to simply scanning all of {@code com.nike.backstopper}. You can
* import this spring config into your main spring config with the {@link Import} annotation to enable {@link
* SpringWebfluxApiExceptionHandler} and {@link SpringWebfluxUnhandledExceptionHandler} in your application. These two
* exception handlers will supersede the built-in spring exception handler chain and will translate ALL errors
* heading to the caller so that they conform to the API error contract. See the {@link
* SpringWebfluxApiExceptionHandler} and {@link SpringWebfluxUnhandledExceptionHandler} classes themselves for more
* info.
*
* Most of the necessary dependencies are setup for autowiring so this configuration class should be sufficient to
* enable Backstopper error handling in your Spring WebFlux application, except for two things:
*
* -
* Backstopper needs to know what your {@link ProjectApiErrors} is. You must expose an instance of that class
* as a dependency-injectable bean (e.g. using {@link Bean} in your Spring WebFlux config). See the javadocs
* for {@link ProjectApiErrors} for more information, and {@link SampleProjectApiErrorsBase} for an example base
* class that sets up all the core errors. Feel free to extend {@link SampleProjectApiErrorsBase} and use it
* directly if the error codes and messages of the core errors it provides are ok for your application).
*
* -
* The {@link ClientDataValidationService} and {@link FailFastServersideValidationService} JSR 303 utility
* services need an injected reference to a {@link Validator}. If you have a JSR 303 Bean Validation
* implementation on your classpath you can just expose that (e.g. via {@link Bean}), otherwise if you
* don't need or want the functionality those services provide you can simply expose
* {@link NoOpJsr303Validator#SINGLETON_IMPL} as your {@link Validator}. Those services would then be
* useless, however if you're not going to use them anyway this would allow you to satisfy the dependency
* injection requirements without pulling in extra jars into your application just to get a {@link Validator}
* impl.
*
*
*
* There are a few critical extension points in Backstopper that you might want to know about for fine tuning what
* errors Backstopper knows how to handle and how your error contract looks. In particular if you want a different set
* of handler listeners for {@link SpringWebfluxApiExceptionHandler} then you should specify a custom {@link
* SpringWebFluxApiExceptionHandlerListenerList} bean to override the default. And if you want to change how the final error contract
* is serialized (and/or what's inside it) for the caller you can specify a custom {@link
* SpringWebfluxApiExceptionHandlerUtils}. There are other extension points for other behavior as well - Backstopper is
* designed to be customizable.
*/
@Configuration
@ComponentScan(basePackageClasses = {
ApiExceptionHandlerBase.class, // Covers the core exception handler base classes, utils, and listener subpackages.
ClientDataValidationService.class, // Covers the JSR 303 service additions.
SpringWebfluxApiExceptionHandler.class, // Covers the Spring WebFlux additions and subclasses.
})
public class BackstopperSpringWebFluxConfig {
}