Sarath
Sarath

Reputation: 3

HTTP Outbound Gateway - Passing URI parameters

I need to invoke a REST API via Spring Integration's HTTP outbound gateway. How do I substitute the path variable with a value from payload. Payload has just one String value. The following code snippet is sending the place holder as such. Any help is appreciated.

@Bean
public MessageHandler   httpGateway(@Value("http://localhost:8080/api/test-resource/v1/{parameter1}/codes") URI uri) {
    HttpRequestExecutingMessageHandler httpHandler = new HttpRequestExecutingMessageHandler(uri);
    httpHandler.setExpectedResponseType(Map.class);
    httpHandler.setHttpMethod(HttpMethod.GET);
    Map<String, Expression> uriVariableExp = new HashMap();

    SpelExpressionParser parser = new SpelExpressionParser();

    uriVariableExp.put("parameter1", parser.parseExpression("payload.Message"));
    httpHandler.setUriVariableExpressions(uriVariableExp);
    return httpHandler;
}

Upvotes: 0

Views: 1024

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121177

Let take a look what @Value is for first of all!

 * Annotation at the field or method/constructor parameter level
 * that indicates a default value expression for the affected argument.
 *
 * <p>Typically used for expression-driven dependency injection. Also supported
 * for dynamic resolution of handler method parameters, e.g. in Spring MVC.
 *
 * <p>A common use case is to assign default field values using
 * "#{systemProperties.myProp}" style expressions.

Looking to your sample there is nothing to resolve as a dependency injection value.

You can just use:

new HttpRequestExecutingMessageHandler("http://localhost:8080/api/test-resource/v1/{parameter1}/codes");

Although it doesn't matter in this case...

You code looks good, unless we don't know what your payload is. That payload.Message expression looks odd. From big height it may mean like MyClass.getMessage(), but it can't invoke the getter because you use a property name as capitalized. If you really have there such a getter, so use it like payload.message. Otherwise, please, elaborate more. Some logs, StackTrace, the info about payload etc... It's fully unclear what is the problem.

Upvotes: 2

Related Questions