我面临 Spring Integration 的问题。我正在尝试通过 HttpRequestExecutingMessageHandler 执行休息调用。我的休息端点仅接受内容类型“application/json”。
问题在于 HttpRequestExecutingMessageHandler 正在以内容类型“text/plain;charset=UTF-8”发布。
@ServiceActivator(inputChannel = "transformRequestToJsonChannel",
outputChannel = "httpRequestOutChannel")
public Message<?> transformRequest(Message<DocumentConverterRequest>
message)
{
LOG.info("transforming document converter request to json: '{}'",
ObjectToJsonTransformer transformer = new ObjectToJsonTransformer();
transformer.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
Object payload = transformer.transform(message).getPayload();
LOG.info("payload: '{}'", payload.toString());
return MessageBuilder.withPayload(payload).build();
}
@Bean
@ServiceActivator(inputChannel = "httpRequestOutChannel")
public HttpRequestExecutingMessageHandler outbound() {
HttpRequestExecutingMessageHandler handler = new
HttpRequestExecutingMessageHandler(documentConverterRestUrl);
handler.setHttpMethod(HttpMethod.POST);
handler.setErrorHandler(httpResponseErrorHandler);
handler.setExpectedResponseType(String.class);
handler.setCharset(Charset.defaultCharset().name());
HeaderMapper<HttpHeaders> mapper = new DefaultHttpHeaderMapper();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE);
mapper.toHeaders(httpHeaders);
handler.setHeaderMapper(mapper);
handler.setOutputChannel(httpResponseChannel());
return handler;
}
如何覆盖内容类型?
这段代码什么也没做:
HeaderMapper<HttpHeaders> mapper = new DefaultHttpHeaderMapper();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE);
mapper.toHeaders(httpHeaders);
当我们收到响应时,会从
toHeaders()
调用 HttpRequestExecutingMessageHandler
。在代码中显式使用它确实没有用,尤其是在 bean 定义阶段以及当您忽略结果时。
您根本不需要使用显式的
HeaderMapper
:默认的就足够了。
ObjectToJsonTransformer
确实将setContentType()
映射到它回复的消息的标题中:
if (headers.containsKey(MessageHeaders.CONTENT_TYPE)) {
// override, unless empty
if (this.contentTypeExplicitlySet && StringUtils.hasLength(this.contentType)) {
headers.put(MessageHeaders.CONTENT_TYPE, this.contentType);
}
}
else if (StringUtils.hasLength(this.contentType)) {
headers.put(MessageHeaders.CONTENT_TYPE, this.contentType);
}
因此,有一个适当的内容类型可以映射。默认情况下
HttpRequestExecutingMessageHandler
使用:
/**
* Factory method for creating a basic outbound mapper instance.
* This will map all standard HTTP request headers when sending an HTTP request,
* and it will map all standard HTTP response headers when receiving an HTTP response.
* @return The default outbound mapper.
*/
public static DefaultHttpHeaderMapper outboundMapper() {
使用一组适当的标头来映射到 HTTP 请求和 HTTP 响应。
new DefaultHttpHeaderMapper()
只为地图带来一组空的标题。
请提出一个问题来改进 JavaDocs 和参考手册,以注意该类的默认构造函数不会将任何标头带到映射中。
@Bean
@ServiceActivator(inputChannel = "httpRequestOutChannel")
public HttpRequestExecutingMessageHandler outbound() {
HttpRequestExecutingMessageHandler handler = Http.outboundGateway(documentConverterRestUrl)
.httpMethod(HttpMethod.POST)
.messageConverters(new MappingJackson2HttpMessageConverter())
.mappedRequestHeaders("Content-Type")
.get();
handler.setOutputChannel(httpResponseChannel());
return handler;
}
我删除了 ObjectToJsonTransformer,因为
messageConverters(new MappingJackson2HttpMessageConverter())
正在做这些事情。
我还必须将
content-type
添加到我的消息标题中:.setHeaderIfAbsent(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
如果我需要将 head 变量添加到 headMapper 中,我该怎么做? 【HttpRequestExecutingMessageHandler】没有getHeaderMap(),当我setHeaderMap()时,它不起作用。在此处输入图像描述