简体   繁体   English

在Spring Integration中将HTTP请求标头设置为LinkedList

[英]HTTP Request Headers set as LinkedList in Spring Integration

We are trying to set the request parameters from an HTTP inbound request to the header. 我们正在尝试设置从HTTP入站请求到标头的请求参数。 We have managed to successfully set the request value to the header, however it is not set in the way we want. 我们已经成功地将请求值设置为标头,但是没有按照我们想要的方式进行设置。

The request header mapping value is set to the message header as a java.util.LinkedList instead of String which is the expected request parameter type. 将请求标头映射值作为java.util.LinkedList而不是预期的请求参数类型的String设置为消息标头。

The following is the configuration 以下是配置

<int-http:inbound-gateway id="inboundApplicationDataRequestGateway"
        supported-methods="GET"
        request-channel="applicationDataRequest"
        reply-channel="applicationDataResponse"
        mapped-response-headers="HTTP_REQUEST_HEADERS"
        path="/services/application/that/data"
        reply-timeout="50000">

            <int-http:header name="dataVersion" expression="#requestParams['data_version']"/>
        </int-http:inbound-gateway>

<int:service-activator id="applicationDataServiceActivator"
                    input-channel="applicationDataRequest"
                    output-channel="applicationDataResponse"
                    ref="dataService"
                    method="getData"
                    requires-reply="false"
                    send-timeout="60000"/>  

The following is the sample service method 以下是示例服务方法

public void getData(Message<?> inMessage){
        MessageHeaders headers = inMessage.getHeaders();
        logger.info("DATA VERSION : " + (String)headers.get("dataVersion"));
    }

The following is the stacktrace 以下是stacktrace

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.messaging.MessageHandlingException: java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.lang.String
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)

How can we have dataVersion set as a string instead of a linked list? 我们如何将dataVersion设置为字符串而不是链接列表?

Also we would need to do the following for POST requests in other http inbound gateway methods as well 同样,我们还需要对其他http入站网关方法中的POST请求执行以下操作

Any help is appreciated. 任何帮助表示赞赏。

Regards, MilindaD 此致MilindaD

That's because servletRequest.getParameterMap() returns Map<String, String[]> and Spring Integration converts it to the MultiValueMap<String, String> . 这是因为servletRequest.getParameterMap()返回Map<String, String[]>而Spring Integration将其转换为MultiValueMap<String, String> And when you ask it to get some value it really returns LinkedList<String> . 当您要求它get某些值时,它实际上会返回LinkedList<String>

So, to get only single value from that List (and here you are really sure that there is only one item) you should use this: 因此,要从该List仅获取单个值(并且您在这里确实确定只有一项),应使用以下命令:

<int-http:header name="dataVersion" expression="#requestParams.getFirst('data_version')"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM