Configure spring boot to redirect 404 to a single page app [duplicate]

This is the full Spring Boot 2.0 example: @Configuration public class WebApplicationConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController(“/notFound”).setViewName(“forward:/index.html”); } @Bean public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerCustomizer() { return container -> { container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, “/notFound”)); }; } }

Enabling CORS globally in Spring Boot

You could indeed define your own Filter as you mentioned in your answer. Spring already has such a CorsFilter already though, so you don’t have to create one yourself. Just register it as a bean and it should work: @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); final CorsConfiguration config = new … Read more

How to I get Spring-Data-MongoDB to validate my objects?

First make sure that you have JSR-303 validator on classpath, for example: <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency> If you use Java config, the way to go is to create 2 beans: @Bean public ValidatingMongoEventListener validatingMongoEventListener() { return new ValidatingMongoEventListener(validator()); } @Bean public LocalValidatorFactoryBean validator() { return new LocalValidatorFactoryBean(); } VoilĂ ! Validation is working now.

How to run Swagger 3 on Spring Boot 3

I had given up and went to use Spring Boot 2.7 after posting the question. But, after seeing Dmitriy’s answer though, I checked Springdoc one last time and found that Springdoc v2 does support Spring Boot 3. Essentially, one has to place the following in their pom: <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.0.0</version> </dependency> Then one can … Read more

Multiple data source and schema creation in Spring Boot

spring.jpa.hibernate.ddl-auto=create has stopped working, not because you have two DataSources, but because your application’s creating its own LocalContainerEntityManagerFactoryBeans. This has the effect of disabling the auto-configuration of a LocalContainerEntityManagerFactoryBean so you now have to configure it yourself. You can configure the two entity managers to have different schema generation behaviour like this (the first’s doing … Read more

How to start Spring Boot app in Spock Integration Test

The problem is that Spock Spring is looking for Spring’s @ContextConfiguration annotation and doesn’t manage to find it. Strictly speaking MyTestSpec is annotated with @ContextConfiguration as it’s a meta-annotation on @SpringApplicationConfiguration but Spock Spring doesn’t consider meta-annotations as part of its search. There’s an issue to address this limitation. In the meantime you can work … Read more

Mono switchIfEmpty() is always called

It’s because switchIfEmpty accepts Mono “by value”. Meaning that even before you subscribe to your mono, this alternative mono’s evaluation is already triggered. Imagine a method like this: Mono<String> asyncAlternative() { return Mono.fromFuture(CompletableFuture.supplyAsync(() -> { System.out.println(“Hi there”); return “Alternative”; })); } If you define your code like this: Mono<String> result = Mono.just(“Some payload”).switchIfEmpty(asyncAlternative()); It’ll always … Read more