0

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?

1 Answers1

0

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

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • thanks for the help! I think that did work only thing which is weird now is I am passing header name as userName but its reading it as username(not as camel case userName). – KeepItSimple Aug 03 '22 at 21:02
  • That's your servlet container feature (Tomcat?). This has nothing to do with Spring Integration already. According to HTTP headers RFC both naming variants represents the same header. In other words the HTTP header is case-agnostic: https://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive – Artem Bilan Aug 03 '22 at 21:04
  • yes its tomcat! I think that explains it, thanks! – KeepItSimple Aug 03 '22 at 21:10