我想运行多个 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 DSL 1.2 起,就有了一个 IntegrationFlowContext
专门用于动态注册 IntegrationFlow
和依赖 bean 的此类用例。
今天发布 GA。
您应该按照这些博客文章中的示例进行操作,并注意 org.springframework.integration.dsl.http.Http
工厂。
但是,事实上,应该尽早这样做。 @PostConstruct
对于这个用例来说是一个很好的阶段。
稍后,HandlerMapping
将无法检测到新的映射。只是因为它在 afterPropertiesSet()
中进行扫描。
关于java - 多个动态 HTTP 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40042366/