简体   繁体   English

对于使用弹簧集成的 GET Rest api 调用,如何读取用户定义的标题?

[英]for GET Rest api call using spring-integration, how to read user defined headers?

MessageHeaders has predefined headers like TIMESTAMP, ERROR_CHANNEL etc. but how to access user defined header? MessageHeaders 具有预定义的标头,例如 TIMESTAMP、ERROR_CHANNEL 等,但如何访问用户定义的 header? My api has http://localhost:8082/load/1234567?source=ABC and headers like username:testuser我的 api 有 http://localhost:8082/load/1234567?source=ABC 和像 username:testuser 这样的标题

message.getPayload() gives me just this 1234567 so that header is not part of payload, but message.getPayload() 只给了我这个 1234567 以便 header 不是有效负载的一部分,但是

Map<String, Object> headers = new HashMap<String, Object>();
Set<String> keys =  message.getHeaders().keySet();
MessageHeaders msgHeader = message.getHeaders();
    for(String key : keys) {
        headers.put(key, msgHeader.get(key));
    }       

& headers.get("username") returns null. & headers.get("username") 返回 null。

could someone please help?有人可以帮忙吗?

I hope that you mean you set a username HTTP header in request.我希望你的意思是你在请求中设置了一个username HTTP header。

The HTTP Inbound Channel Adapter (or Gateway) come with a DefaultHttpHeaderMapper by default. HTTP 入站通道适配器(或网关)默认带有DefaultHttpHeaderMapper This one does only standard HTTP Request headers mapping by default:这个默认只做标准的 HTTP 请求头映射:

private static final String[] HTTP_REQUEST_HEADER_NAMES =
        {
                HttpHeaders.ACCEPT,
                HttpHeaders.ACCEPT_CHARSET,
                HttpHeaders.ACCEPT_ENCODING,
                HttpHeaders.ACCEPT_LANGUAGE,
                HttpHeaders.ACCEPT_RANGES,
                HttpHeaders.AUTHORIZATION,
                HttpHeaders.CACHE_CONTROL,
                HttpHeaders.CONNECTION,
                HttpHeaders.CONTENT_LENGTH,
                HttpHeaders.CONTENT_TYPE,
                HttpHeaders.COOKIE,
                HttpHeaders.DATE,
                HttpHeaders.EXPECT,
                HttpHeaders.FROM,
                HttpHeaders.HOST,
                HttpHeaders.IF_MATCH,
                HttpHeaders.IF_MODIFIED_SINCE,
                HttpHeaders.IF_NONE_MATCH,
                HttpHeaders.IF_RANGE,
                HttpHeaders.IF_UNMODIFIED_SINCE,
                HttpHeaders.MAX_FORWARDS,
                HttpHeaders.PRAGMA,
                HttpHeaders.PROXY_AUTHORIZATION,
                HttpHeaders.RANGE,
                HttpHeaders.REFERER,
                HttpHeaders.TE,
                HttpHeaders.UPGRADE,
                HttpHeaders.USER_AGENT,
                HttpHeaders.VIA,
                HttpHeaders.WARNING
        };

To include your custom header into a message this channel adapter produces, you just need to incorporate this configuration option:要将您的自定义 header 包含到此通道适配器生成的消息中,您只需合并此配置选项:

/**
 * Provide the pattern array for request headers to map.
 * @param patterns the patterns for request headers to map.
 * @return the current Spec.
 * @see DefaultHttpHeaderMapper#setOutboundHeaderNames(String[])
 */
public S mappedRequestHeaders(String... patterns) {

and use, for example, just * to map all the headers, or if your requirements are strict only for your headers, then pass their names over there.并使用例如*到 map 所有标题,或者如果您的要求仅对您的标题严格,则将它们的名称传递到那里。

See more info in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping在文档中查看更多信息: https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping

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

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