Spring 关于如何组合多个变量的两条路径的集成

Spring Integration on how to combine both paths for mutiple variables

我想弄清楚如何为 .payLoadExpression 引入多个路径变量,但还没有想出办法来做到这一点。我还需要为 .uriVariable 做些什么吗?

这只适用于一个 .payloadExpression

@Bean
public IntegrationFlow getDrugsByIngredientWorkflow() {
  return IntegrationFlows
        .from(Http.inboundGateway("/drugsbyingcode/name/{name}")
        .payloadExpression("#pathVariables.name")
        .requestMapping(m -> m.methods(HttpMethod.GET))
        .errorChannel(IntegrationConfiguration.CHANNEL_ERROR_REST))
        .handle(Http.outboundGateway(url + "/" + "{name}")
        .charset("UTF-8")
        .httpMethod(HttpMethod.GET)
        .uriVariable("name", "payload")
        .expectedResponseType(DrugByIngredientResponse.class))
        .transform(DrugByIngredientResponse::getDrug)
        .get();
}

这不起作用

public IntegrationFlow getContraindicationsByDrugcodeAndIcd10WorkFlow() {
  return IntegrationFlows
         .from(Http.inboundGateway("/drug/{code}/icd10/{icd10}/contraindications")
         .payloadExpression("#pathVariables.code" + ',' + "#pathVariables.icd10")
         .requestMapping(m -> m.methods(HttpMethod.GET))
         .errorChannel(IntegrationConfiguration.CHANNEL_ERROR_REST))
         .handle(Http.outboundGateway(url + "/{code}/V22?format=json")
         .httpMethod(HttpMethod.GET)
         .uriVariable("code", "payload")
         .expectedResponseType(String.class))
         .get();
    }

"Does not work" 对这样的问题帮助不大;您需要准确描述您遇到的问题。

如果您使用更好的缩进方案使您的代码更易于阅读,这也会有所帮助。

我们得到了很多这样的问题,只有时间浏览配置。

问题是您使用 java 连接两个变量而不是 SpEL:

@Bean
public IntegrationFlow getContraindicationsByDrugcodeAndIcd10WorkFlow() {
    return IntegrationFlows
            .from(Http.inboundGateway("/drug/{code}/icd10/{icd10}/contraindications")
                    .payloadExpression("#pathVariables.code + ',' + #pathVariables.icd10")
                    .requestMapping(m -> m.methods(HttpMethod.GET)))
            .handle(Http.outboundGateway(url + "/{code}/V22?format=json")
                    .httpMethod(HttpMethod.GET)
                    .uriVariable("code", "payload")
                    .expectedResponseType(String.class))
            .get();
}

结果

2019-07-31 09:17:06.210 DEBUG 36050 --- [nio-8080-exec-1] o.s.integration.channel.DirectChannel : preSend on channel ... message: GenericMessage [payload=fiz,buz, headers={http_requestMethod=GET, ...

您还可以将路径变量映射到消息 headers 并在下游 uriVariables.

中使用它们

对于你的版本,SpEL 不知道如何处理逗号。

#foo,#bar

是无效语法,必须是

#foo + ',' + #bar

需要使用构造函数和getter/setters创建一个新的class。还要注意,您也可以在句柄请求上定义两个.uriVariable

   public IntegrationFlow getContraindicationsByDrugcodeAndIcd10WorkFlow() {
       //new ContraindicationRequest("string", "string");
       return IntegrationFlows
               .from(Http.inboundGateway("/contraindicationsbydrugcodeandicd10/{code}/{icd10}")
                       .payloadExpression("new net.eir.nexus.model.dto.api.dit.request.ContraindicationRequest(#pathVariables.code, #pathVariables.icd10)")
                       .requestMapping(m -> m.methods(HttpMethod.GET))
                       .errorChannel(IntegrationConfiguration.CHANNEL_ERROR_REST)
               )
               `enter code here`///dit/contraindicationsbydrugcodes/1234567890/Y12319,Y20743/Z34,I10,E0810?icdtype=icd10&format=json
               .handle(Http.outboundGateway(url + "/{code}/{icd10}?icdtype=icd10&format=json")
                       .httpMethod(HttpMethod.GET)
                       .uriVariable("code", "payload.drugCode")
                       .uriVariable("icd10", "payload.icd10")
                       .expectedResponseType(ContraindicationsByDrugcodeAndIcd10Response.class))
                 .transform(ContraindicationsByDrugcodeAndIcd10Response::getAllergyIngredient)
               .get();


关于如何做这样一件简单的事情,令人惊讶的是缺乏好的和有用的文档,这是 Google 上出现的主要页面之一,实际上是唯一解决问题的页面之一直接问题,所以我会找到我花了很长时间才找到的解决方案,希望它能帮助其他正在寻找相同问题的人。

简而言之,使用 headerExpression() 而不是 payloadExpression()

@Bean
IntegrationFlow mySpringIntegrationEndpointMethod(
        EntityManagerFactory entityManagerFactory,
        DefaultHttpHeaderMapper headerMapper
) {
    return IntegrationFlows
            .from(Http.inboundGateway("/some/path/with/{my_var_1}/and/{my_var_2}")
                    .requestMapping(mapping -> mapping.methods(HttpMethod.GET))
                    .headerExpression("myVar1", "#pathVariables.my_var_1")
                    .headerExpression("myVar2", "#pathVariables.my_var_2")
                    .headerMapper(headerMapper)
                    .errorChannel("somechannel")
                    .get())
            .handle(Jpa.retrievingGateway(entityManagerFactory)
                    .nativeQuery("SELECT * FROM my_function(:myVar1, :myVar2)")
                    .parameterExpression("myVar1","new Integer(headers['myVar1'])")
                    .parameterExpression("myVar2", "new Integer(headers['myVar2'])"))
            .get();
}