Search code examples
javaspring-integrationdslspring-integration-dsl

Spring Integration on how to combine both paths for mutiple variables


I am trying to figure out how to bring in multiple pathvariables for the .payLoadExpression but havent figures out a way to do that. Do i also have to do something for the .uriVariable?

This works with just one .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();
}

This does not work

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();
    }

Solution

  • A new class needs to be created with a constructor and getter/setters.. also note that you can define two .uriVariable on the handle request as well.

       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();