HTTP出站网关 - 传递URI参数

HTTP出站网关 - 传递URI参数

问题描述:

我需要通过Spring Integration的HTTP出站网关调用REST API。如何用有效载荷中的值替换路径变量。 Payload只有一个字符串值。下面的代码片段发送占位符。任何帮助表示赞赏。HTTP出站网关 - 传递URI参数

@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; 
} 

让我们来看看@Value首先是什么!

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

展望您的示例没有什么可以解决的依赖注入值。

你可以使用:

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

虽然没有在这种情况下,重要...

你的代码看起来不错,除非我们不知道你的​​是什么。 那payload.Message表情看起来很奇怪。从很大的高度来看,它可能意味着像MyClass.getMessage(),但它不能调用getter,因为您使用大写的属性名称。如果你真的有这样一个吸气剂,那么就像payload.message一样使用它。否则,请详细说明。一些日志,StackTrace,关于​​等的信息......完全不清楚是什么问题。

+0

谢谢阿尔乔姆。现在工作正常。我已经将url移至application.properties并更新了如下所示的表达式uriVariableExp.put(“parameter1”,parser.parseExpression(“payload”)); – Sarath