1

I have to write Spring Integration flow for the below steps:

(1) Read json files from a directory

(2) Transform to Java object and add header

(3) Transform to JSON

(4) Post to end point: POST http://127.0.0.1:8081/v1/userValidation

(5) Extract response to a Java Object called 'UserValidationResponse'. The response has a field called orderID that will be used in step 7.

(6) Write output to a channel called 'userValidationPostOutputWriterChannel'. This channel will log the Http Status.

(7) PUT on end point: PUT http://127.0.0.1:8081/v1/userValidation/{orderID extracted in step 5}/cancel.

(8) Write output to a channel called 'userValidationPostCancelOutputWriterChannel'. This channel will log the Http Status.

WHAT I NEED:

How to construct the URL http://127.0.0.1:8081/v1/userValidation/{orderID}/cancel dynamically?

I looked it up online and found that I have to use a uri param.

Can you please give me an example programmatically?

I could only find examples using config XML, and I have NO config XML in my code.

SAMPLE CODE:

@Bean
public IntegrationFlow placeUserRequest(RestTemplate restTemplate) {
private String AUTHORIZATION = "USER";

    return IntegrationFlows.from(sourceDirectory(), configurer -> configurer.poller(Pollers.fixedDelay(10000)))
            .transform(Transformers.fromJson(UserRequest.class))
            .enrichHeaders(h -> h.header("Content-Type", "application/json"))
            .transform(Transformers.toJson())
            .handle(Http.outboundGateway("http://127.0.0.1:8081/v1/userValidation", restTemplate)
                    .httpMethod(HttpMethod.POST)
                    .mappedRequestHeaders("auth*", "TraceabilityID", AUTHORIZATION)
                    .charset("UTF-8")
                    .expectedResponseType(UserValidationResponse.class))
            .wireTap(flow -> flow.channel("userValidationPostOutputWriterChannel"))
            .handle(Http.outboundGateway("http://127.0.0.1:8081/v1/userValidation/" + parser.parseExpression("payload.getOrderID()") + "/cancel", restTemplate)
                    .httpMethod(HttpMethod.PUT)
                    .mappedRequestHeaders("auth*", "TraceabilityID", AUTHORIZATION)
                    .charset("UTF-8").expectedResponseType(Map.class))
            .wireTap(flow -> flow.channel("userValidationPostCancelOutputWriterChannel"))
            .get();
}

SAMPLE RESPONSE FROM POST http://127.0.0.1:8081/v1/userValidation:

{
    "status": "CREATED", 
    "orderID": "78e323f7-d3f9-11e9-a71a-035a2a38a4e0",
    "userData" : {
            "userFName": "John",
            "userLName": "Smith",
            "userLocation": "Florida"
        },
    "userAccess": "USER"    
}

The next execution should be a PUT http://127.0.0.1:8081/v1/userValidation/78e323f7-d3f9-11e9-a71a-035a2a38a4e0/cancel.

1
  • Artem Bilan / Gary Russell - Will you please be able to help? Commented Sep 10, 2019 at 20:31

1 Answer 1

2

It is fully OK to have that URI on the HTTP Outbound Gateway configured like this:

 http://127.0.0.1:8081/v1/userValidation/{orderID}/cancel

This is called template. You have there that orderID uriVariable. The Http.outboundGateway has support for that as well:

/**
 * Specify a value SpEL expression for the uri template variable.
 * @param variable the uri template variable.
 * @param expression the expression to evaluate value for te uri template variable.
 * @return the current Spec.
 * @see AbstractHttpRequestExecutingMessageHandler#setUriVariableExpressions(Map)
 */
public S uriVariable(String variable, String expression) {

So, for your use-case it must be something like this:

 .handle(Http.outboundGateway("http://127.0.0.1:8081/v1/userValidation/{orderID}/cancel", restTemplate)
                .httpMethod(HttpMethod.PUT)
                .mappedRequestHeaders("auth*", "TraceabilityID", AUTHORIZATION)
                .charset("UTF-8").expectedResponseType(Map.class)
                .uriVariable("orderID", "payload.orderID"))
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to somehow extract uriVariable parameters value directly from payload object? i.e. ((UserRequest)message.getPayload())).getOrderID()? Using expressions seems a little bit inflexible e.g. in terms of later refactoring
See overloaded variant with function instead of expression

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.