简体   繁体   中英

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? My api has http://localhost:8082/load/1234567?source=ABC and headers like username:testuser

message.getPayload() gives me just this 1234567 so that header is not part of payload, but

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.

could someone please help?

I hope that you mean you set a username HTTP header in request.

The HTTP Inbound Channel Adapter (or Gateway) come with a DefaultHttpHeaderMapper by default. This one does only standard HTTP Request headers mapping by default:

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:

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

See more info in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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