Spring Integration - How to use uri param / url expression programmatically?

Deepboy :

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.

Artem Bilan :

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"))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=328799&siteId=1