【问题标题】:Multiple dynamic HTTP endpoints多个动态 HTTP 端点
【发布时间】:2016-10-14 11:44:16
【问题描述】:

我想运行多个应该基于路径列表创建的 HTTP 端点。

目前我可以创建一个端点:

@MessagingGateway(defaultRequestChannel = "requestChannel")
public interface Gateway {
    String sendReceive(String in);
}

@Bean
public MessageChannel requestChannel() {
    return new DirectChannel();
}

@Bean
public IntegrationFlow flow() {
    return IntegrationFlows.from("requestChannel").transform(new ObjectToStringTransformer())
            .handle(new MyHandle())
            .get();
}

@Bean
public HttpRequestHandlingMessagingGateway httpGate() {
    HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
    RequestMapping mapping = new RequestMapping();
    mapping.setMethods(HttpMethod.POST);
    mapping.setPathPatterns("/path");
    gateway.setRequestMapping(mapping);
    gateway.setRequestChannel(requestChannel());
    gateway.setRequestPayloadType(byte[].class);
    return gateway;
}

但我想做这样的事情:

@Autowired
List<String> paths;

@PostConstruct
public void createEndpoints() {
    for (String path : paths) {
        //code for dynamic endpoint creation
    }
}

private class MyHandle extends AbstractReplyProducingMessageHandler {

    @Override
    protected Object handleRequestMessage(Message<?> requestMessage) {
        return this.getMessageBuilderFactory().withPayload("Your message: " + requestMessage.getPayload());
    }
}

你能告诉我该怎么做吗?

【问题讨论】:

    标签: java spring spring-boot spring-integration


    【解决方案1】:

    从 Java DSL 1.2 开始,有一个 IntegrationFlowContextexactly 用于动态注册 IntegrationFlow 和依赖 bean 的用例。

    https://spring.io/blog/2016/09/27/java-dsl-for-spring-integration-1-2-release-candidate-1-is-available

    今天正式发布。

    您应该按照这些博客文章中的示例进行操作,并注意org.springframework.integration.dsl.http.Http 工厂。

    但是,确实,请尽早这样做。 @PostConstruct 是这个用例的好阶段。 稍后,HandlerMapping 将无法检测到新的映射。仅仅因为它在其afterPropertiesSet() 中进行扫描。

    【讨论】:

      猜你喜欢
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 2020-05-31
      • 2021-12-13
      • 1970-01-01
      相关资源
      最近更新 更多