Spring 集成 HttpRequestExecutingMessageHandler ContentType 问题
Spring Integration HttpRequestExecutingMessageHandler ContentType Issue
我遇到了 Spring 集成的问题。我正在尝试通过 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()
映射到它回复的消息的 headers 中:
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() {
使用一组适当的 headers 映射到 HTTP 请求和 HTTP 响应。
new DefaultHttpHeaderMapper()
仅将一组空的 headers 引入映射。
请提出问题以改进 JavaDocs 和参考手册,以注意 class 的默认构造函数不会带来任何 headers 映射。
@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
header:.setHeaderIfAbsent(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
我遇到了 Spring 集成的问题。我正在尝试通过 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()
映射到它回复的消息的 headers 中:
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() {
使用一组适当的 headers 映射到 HTTP 请求和 HTTP 响应。
new DefaultHttpHeaderMapper()
仅将一组空的 headers 引入映射。
请提出问题以改进 JavaDocs 和参考手册,以注意 class 的默认构造函数不会带来任何 headers 映射。
@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
header:.setHeaderIfAbsent(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)