使用HttpRequestExecutingMessageHandler调用REST接口遇转换器问题,已尝试方案未解决
Hey there, let's tackle this frustrating issue you're hitting when using Spring Integration's HttpRequestExecutingMessageHandler for POST requests. That error about missing HttpMessageConverter usually boils down to the handler not being configured to use JSON serialization properly—even if you have Jackson on the classpath. Here are actionable steps to fix this:
1. Explicitly Configure Jackson Message Converter for the Handler
By default, HttpRequestExecutingMessageHandler might not automatically pick up Jackson converters unless you explicitly register them. Create a MappingJackson2HttpMessageConverter bean and attach it to your handler:
@Configuration @EnableIntegration @IntegrationComponentScan public class IntegrationConfig { @Bean public MappingJackson2HttpMessageConverter jacksonMessageConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON)); return converter; } @Bean public HttpRequestExecutingMessageHandler restPostHandler() { HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("https://your-rest-endpoint.com/post"); handler.setHttpMethod(HttpMethod.POST); // Register the Jackson converter here handler.setMessageConverters(Collections.singletonList(jacksonMessageConverter())); // Ensure we're expecting JSON response too (optional but recommended) handler.setExpectedResponseType(YourResponseClass.class); return handler; } // Define your integration flow using this handler @Bean public IntegrationFlow restFlow() { return IntegrationFlow.from("restPostChannel") .handle(restPostHandler()) .get(); } }
2. Set the Correct Content-Type Header on Your Message
The error mentions application/x-java-serialized-object, which suggests the outgoing message has a content-type forcing Java object serialization instead of JSON. Make sure your message includes the right header:
// When sending a message to the channel YourRequestPayload payload = new YourRequestPayload(); Message<YourRequestPayload> message = MessageBuilder.withPayload(payload) .setHeader(MessageHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .build(); restPostChannel.send(message);
Alternatively, set a default content-type on the handler to avoid adding it to every message:
handler.setDefaultHeaders(Collections.singletonMap(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
3. Verify Spring Integration's Conversion Service Setup
If you're using a custom ConversionService for your message channels, ensure it includes Jackson support:
@Bean public ConversionService integrationConversionService() { DefaultConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new MappingJackson2HttpMessageConverter()); return conversionService; } // Attach it to your handler handler.setConversionService(integrationConversionService());
4. Double-Check Jackson Dependencies
Even though you noted having Jackson data-bind, confirm your build file has the correct dependencies. For Maven:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> <!-- Use a version compatible with your Spring stack --> </dependency>
For Gradle:
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
5. Ensure Payload Class is Jackson-Friendly
Make sure your request payload class is set up for Jackson serialization—add annotations if you need to handle unknown fields or custom naming:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class YourRequestPayload { private String name; private int age; // Getters and setters required for Jackson serialization }
Try these steps one by one—explicitly setting the message converter and content-type header usually resolves this issue. If you're still stuck, check if any custom HttpMessageConverter beans are overriding the default setup, or if your payload has a type Jackson can't serialize out of the box.
内容的提问来源于stack exchange,提问作者AbNig

