【问题标题】:spring XD - http-client add application/json headerspring XD - http-client 添加应用程序/json 标头
【发布时间】:2016-07-04 21:51:27
【问题描述】:

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

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

不幸的是,由于http 模块仅将有效负载发送到httpclient,因此httpclient 由于缺少 content-type 标头而返回 415 错误。

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

我怎样才能实现这样的目标?

非常感谢您的帮助。

编辑

我刚刚发现 httpclient 处理器 (link) 支持 headersExpression 用于派生要使用的 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?

【问题讨论】:

    标签: spring-integration spring-xd spring-el spring-cloud-dataflow


    【解决方案1】:

    有关 StackTrace 的更多信息,请参阅 GH issue

    首先,它不再是 Spring XD。 Spring Cloud Dataflow 是不同的产品,它的行为可能与之前的 Spring XD 不同。

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

    现在谈谈问题。看:

    标题表达式

     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'}
    

    【讨论】:

    • 首先非常感谢您的支持!我将仔细检查是否可以在 Bluemix 上正确升级部署新版本。关于这里的问题,您的解决方案是完美的!只是为了完整起见,使用 shell 部署您需要转义特殊字符的流,使其变为:--headers-expression={\'Content\-type\':\'application\/json\'}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 2014-07-30
    • 2017-08-29
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多