spring XD - http-client添加application/json头文件



我已经在PaaS环境中安装了Spring XD,由于遗留问题,我坚持使用1.0.0.M1版本。(见参考文档)。我的目标是使用http-client模块调用http rest API。我的流定义是:

http | httpclient --url='''<my_url>''' --http-method=POST --mappedRequestHeaders=HTTP_REQUEST_HEADERS | log --expression=#root

不幸的是,由于http模块只向httpclient发送有效载荷,httpclient由于缺少内容类型报头而返回415错误。

考虑到我既不能添加新模块也不能修改现有模块(在这样的版本中,您只能引用spring存储库),我想使用tranform模块来注入内容类型头。

我怎样才能达到这样的目标?

非常感谢你的帮助。

编辑:

我刚刚发现httpclient处理器(链接)支持headerexpression一个用于派生http头映射使用的SpEL表达式。然而:

--headers-expression='{Content-Type:'application/json'}'

给出以下解析异常:

    org.springframework.expression.spel.SpelEvaluationException: EL1008E:
(pos 1): Property or field 'Content' cannot be found on object of type 'org.springframework.messaging.support.GenericMessage' - maybe not public?

查看GH问题获取更多StackTrace。

首先它不再是Spring XD了。Spring Cloud Dataflow是一个不同的产品,它的行为可能与你以前使用Spring XD时不同。

第二:它已经是1.0.0.RC1版本了。所以,考虑升级。

现在来看这个问题。看:

headersExpression

 A SpEL expression used to derive the http headers map to use.

因此,这个表达式必须返回一个Map,并由下面的代码进行确认:

if (properties.getHeadersExpression() != null) {
    Map<?, ?> headersMap = properties.getHeadersExpression().getValue(message, Map.class);

现在让我们来看看这个问题:

org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 1): Property or field 'Content' cannot be found on object of type 'org.springframework.messaging.support.GenericMessage' - maybe not public?
 org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224) ~[spring-expression-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
 org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94) ~[spring-expression-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
 org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81) ~[spring-expression-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
 org.springframework.expression.spel.ast.OpMinus.getValueInternal(OpMinus.java:98) ~[spring-expression-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]

OpMinus是根本原因。因此,SpEL将Content-Type表达式视为minus运算符。

当然是悲哀的,但是解决方法就像把key也包装成引号一样:

--headers-expression={'Content-Type':'application/json'}

最新更新