@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ServerNameMapping {
String[] value();
}
public class ServerNameRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
@Override
protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
ServerNameMapping serverNameMapping = AnnotationUtils.findAnnotation(handlerType, ServerNameMapping.class);
if (serverNameMapping != null) {
return new ServerNameRequestConditional(new HashSet<>(List.of(serverNameMapping.value())));
}
return super.getCustomTypeCondition(handlerType);
}
@Override
protected RequestCondition<?> getCustomMethodCondition(Method method) {
ServerNameMapping serverNameMapping = AnnotationUtils.findAnnotation(method, ServerNameMapping.class);
if (serverNameMapping != null) {
return new ServerNameRequestConditional(new HashSet<>(List.of(serverNameMapping.value())));
}
return super.getCustomMethodCondition(method);
}
}
@ServerNameMapping("${app.externalApiHostname}")
@RequestMapping("/api/v1")
@RestController
public class ExternalApiController {
@GetMapping
public void test() {
System.out.println("ExternalApiController");
}
}
SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
Expression expression = spelExpressionParser.parseExpression("getAge() > 18");
String value = expression.getValue(object, String.class);
public class ServerNameRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
private final EmbeddedValueResolver embeddedValueResolver;
public ServerNameRequestMappingHandlerMapping(ConfigurableBeanFactory beanFactory) {
this.embeddedValueResolver = new EmbeddedValueResolver(beanFactory);
}
@Override
protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
ServerNameMapping serverNameMapping = AnnotationUtils.findAnnotation(handlerType, ServerNameMapping.class);
if (serverNameMapping != null) {
String host = embeddedValueResolver.resolveStringValue(serverNameMapping.value());
if (host == null) throw new RuntimeException("Not passed hostname param");
System.out.println(new HashSet<>(List.of(host)));
return new ServerNameRequestConditional(new HashSet<>(List.of(host)));
}
return super.getCustomTypeCondition(handlerType);
}
@Override
protected RequestCondition<?> getCustomMethodCondition(Method method) {
ServerNameMapping serverNameMapping = AnnotationUtils.findAnnotation(method, ServerNameMapping.class);
if (serverNameMapping != null) {
return new ServerNameRequestConditional(new HashSet<>(List.of(serverNameMapping.value())));
}
return super.getCustomMethodCondition(method);
}
}