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?
CodePudding user response:
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