0

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();
    }
IQbrod
  • 2,060
  • 1
  • 6
  • 28
  • @GaryRussell I guess my issue is i do not know how to send in two path variables so the intergration knows to put the code in ```{code}``` and ```{icd10}``` – Nick Stepka Jul 31 '19 at 12:45

3 Answers3

1

There's a surprising lack of good and helpful documentation on how to do such a simple thing, and this is one of the main pages that shows up on Google and in fact one of the only ones that addresses the question directly, so I will find the solution that took me way too long to find, hoping it will help others who are looking for the same.

In a nutshell, use headerExpression() instead of 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();
}
Chris
  • 4,212
  • 5
  • 37
  • 52
0

"Does not work" is not very helpful for questions like this; you need to describe exactly what problems you are having.

It would also help if you used a better indentation scheme to make your code easier to read.

We get loads of these questions and only have time to skim the configuration.

The problem is you are using java to concatenate the two variables instead of 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();
}

Result

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, ...

You can also map path variables to message headers and use them in the downstream uriVariables.

With your version, SpEL doesn't know what to do with the comma.

#foo,#bar

is invalid syntax, it must be

#foo + ',' + #bar
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • A working solution for what exactly? This question was about SpEL syntax and the last line of my answer describes the proper syntax. The other answers provide other techniques to get multiple inputs into the message. If something is not clear, I suggest you ask a new question showing your code and configuration. – Gary Russell Mar 29 '21 at 12:43
0

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