From 0203152f3f65f72fb3af9fce264694d3958d36b7 Mon Sep 17 00:00:00 2001 From: Timon Back Date: Fri, 31 May 2024 18:51:20 +0200 Subject: [PATCH] chore(core): allow missing WebMvcConfigurer class in classpath --- .../SpringwolfUiResourceConfiguration.java | 40 +++--------------- .../SpringwolfUiResourceConfigurer.java | 41 +++++++++++++++++++ 2 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfigurer.java diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfiguration.java b/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfiguration.java index 030dffabc..3b6c363a0 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfiguration.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfiguration.java @@ -1,40 +1,12 @@ +// SPDX-License-Identifier: Apache-2.0 package io.github.springwolf.core.configuration; -import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties; -import lombok.RequiredArgsConstructor; -import org.springframework.boot.autoconfigure.web.WebProperties; -import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Configuration; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.context.annotation.Import; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - @Configuration -@RequiredArgsConstructor -// Highest so that all others will replace this configuration -@Order(value = Ordered.HIGHEST_PRECEDENCE) -public class SpringwolfUiResourceConfiguration implements WebMvcConfigurer { - - private final SpringwolfConfigProperties springwolfConfigProperties; - private final WebProperties webProperties; - private final WebMvcProperties webMvcProperties; - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler(springwolfConfigProperties.getPath().getBase() + "/**", webMvcProperties.getStaticPathPattern()) - .addResourceLocations(buildStaticLocation()); - } - - private String[] buildStaticLocation() { - List staticLocations = new ArrayList<>(Arrays.asList(webProperties.getResources().getStaticLocations())); - staticLocations.add("classpath:/META-INF/resources/springwolf/"); - - return staticLocations.toArray(new String[0]); - } - -} +@ConditionalOnClass(WebMvcConfigurer.class) +@Import(SpringwolfUiResourceConfigurer.class) +public class SpringwolfUiResourceConfiguration {} diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfigurer.java b/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfigurer.java new file mode 100644 index 000000000..8757a23c4 --- /dev/null +++ b/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfigurer.java @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: Apache-2.0 +package io.github.springwolf.core.configuration; + +import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties; +import lombok.RequiredArgsConstructor; +import org.springframework.boot.autoconfigure.web.WebProperties; +import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@Configuration +@RequiredArgsConstructor +@Order(value = Ordered.HIGHEST_PRECEDENCE) // Highest so that all others will replace this configuration +public class SpringwolfUiResourceConfigurer implements WebMvcConfigurer { + + private final SpringwolfConfigProperties springwolfConfigProperties; + private final WebProperties webProperties; + private final WebMvcProperties webMvcProperties; + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler( + springwolfConfigProperties.getPath().getBase() + "/**", webMvcProperties.getStaticPathPattern()) + .addResourceLocations(buildStaticLocation()); + } + + private String[] buildStaticLocation() { + List staticLocations = + new ArrayList<>(Arrays.asList(webProperties.getResources().getStaticLocations())); + staticLocations.add("classpath:/META-INF/resources/springwolf/"); + + return staticLocations.toArray(new String[0]); + } +}