我遇到了Spring Integration问题。我试图通过HttpRequestExecutingMessageHandler执行rest调用。我的其余端点仅接受内容类型“ 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;
}
如何覆盖内容类型?
答案 0 :(得分:0)
这段代码不执行任何操作:
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和参考手册,以注意该类的默认ctor不会带来任何标头。
答案 1 :(得分:0)
and True or False
我删除了ObjectToJsonTransformer,因为@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;
}
在做这些事情。
我还必须将messageConverters(new MappingJackson2HttpMessageConverter())
添加到我的消息标题中:content-type