#973 - Explicit configuration options for web stacks.

@EnableHypermediaSupport now exposes a "stacks" attribute to limit the enablement of hypermedia functionality to a certain web stack.
This commit is contained in:
Oliver Drotbohm
2019-04-02 15:42:28 +02:00
parent 0e02d4f041
commit f27160d16f
4 changed files with 139 additions and 9 deletions

View File

@@ -26,7 +26,9 @@ import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.support.WebStack;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
/**
* Activates hypermedia support in the {@link ApplicationContext}. Will register infrastructure beans to support all
@@ -49,6 +51,15 @@ public @interface EnableHypermediaSupport {
*/
HypermediaType[] type();
/**
* Configures which {@link WebStack}s we're supposed to enable support for. By default we're activating it for all
* available ones if they happen to be in use. Configure this explicitly in case you're using WebFlux components like
* {@link WebClient} but don't want to use hypermedia operations with it.
*
* @return
*/
WebStack[] stacks() default { WebStack.WEBMVC, WebStack.WEBFLUX };
/**
* Hypermedia representation types supported.
*

View File

@@ -15,8 +15,10 @@
*/
package org.springframework.hateoas.config;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
@@ -29,23 +31,44 @@ import org.springframework.hateoas.support.WebStack;
*/
class WebStackImportSelector implements ImportSelector {
private static final String WEB_STACK_MISSING = "At least one web stack has to be selected in @EnableHypermediaSupport on %s!";
static Map<WebStack, String> CONFIGS;
static {
Map<WebStack, String> configs = new HashMap<>();
configs.put(WebStack.WEBMVC, "org.springframework.hateoas.config.WebMvcHateoasConfiguration");
configs.put(WebStack.WEBFLUX, "org.springframework.hateoas.config.WebFluxHateoasConfiguration");
CONFIGS = Collections.unmodifiableMap(configs);
}
/*
* (non-Javadoc)
* @see org.springframework.context.annotation.ImportSelector#selectImports(org.springframework.core.type.AnnotationMetadata)
*/
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
public String[] selectImports(AnnotationMetadata metadata) {
List<String> imports = new ArrayList<>();
Map<String, Object> attributes = metadata.getAnnotationAttributes(EnableHypermediaSupport.class.getName());
if (WebStack.WEBMVC.isAvailable()) {
imports.add("org.springframework.hateoas.config.WebMvcHateoasConfiguration");
// Configuration class imported but not through @EnableHypermediaSupport
if (attributes == null) {
return new String[0];
}
if (WebStack.WEBFLUX.isAvailable()) {
imports.add("org.springframework.hateoas.config.WebFluxHateoasConfiguration");
WebStack[] stacks = (WebStack[]) attributes.get("stacks");
if (stacks.length == 0) {
throw new IllegalStateException(String.format(WEB_STACK_MISSING, metadata.getClassName()));
}
return imports.toArray(new String[0]);
return Arrays.stream(stacks) //
.filter(WebStack::isAvailable) //
.map(CONFIGS::get) //
.toArray(String[]::new);
}
}