spring 将自定义对象传递给 HttpRequestExecutingMessageHandler 时集成 406 null

spring integration 406 null when passing a custom object to HttpRequestExecutingMessageHandler

我正在使用 HttpRequestExecutingMessageHandler 向本地服务发出 POST 请求。

服务如下:-

@RestController
@RequestMapping("/accountbalance")
public class AccountBalanceController {

    @Autowired
    private AccountBalanceDetailsRepo accountBalanceDetailsRepo;

    @RequestMapping(method = RequestMethod.POST, path = "/exp")
    public List<AccountBalanceDetails> getAccounts(@RequestBody User name) {
        System.out.println(">>>>>>>>>>> " + name);
        return accountBalanceDetailsRepo.findAll();
    }
}

用户 class 是:-

public class User implements Serializable {

    private String name;
    private Integer age;

//getters and setters
}

spring 集成配置为:

@Bean
    public HttpRequestExecutingMessageHandler httpOutBound(@Qualifier("httpOutChannel") DirectChannel outputChannel) {
        HttpRequestExecutingMessageHandler httpRequestExecutingMessageHandler = new
                HttpRequestExecutingMessageHandler("http://localhost:8080/accountbalance/exp");
        httpRequestExecutingMessageHandler.setHttpMethod(HttpMethod.POST);
        httpRequestExecutingMessageHandler.setExpectedResponseType(ArrayList.class);
        httpRequestExecutingMessageHandler.setMessageConverters(Arrays.asList(new SerializingHttpMessageConverter()));
        httpRequestExecutingMessageHandler.setOutputChannel(outputChannel);
        return httpRequestExecutingMessageHandler;
    }

    @Bean
    public IntegrationFlow httpFlow(@Qualifier("httpReqChannel") DirectChannel directChannel,
                                    HttpRequestExecutingMessageHandler messageHandler) {
        return IntegrationFlows.from(directChannel).handle(messageHandler).get();
    }

    @Bean
    public DirectChannel httpReqChannel() {
        return new DirectChannel();
    }

    @Bean
    public DirectChannel httpOutChannel() {
        return new DirectChannel();
    }

    @Bean
    public IntegrationFlow httpOutFlow(@Qualifier("httpOutChannel") DirectChannel directChannel) {
        return IntegrationFlows.from(directChannel).handle(msg -> System.out.println(">>>>> "+ msg)).get();
    }

在主class我发消息到httpReqChannel频道如下:-

User user = new User();
        user.setAge(29);
        user.setName("Amar");
        directChannel.send(MessageBuilder.withPayload(user).build())

我得到以下异常:-

Caused by: org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [http://localhost:8080/accountbalance/exp]; nested exception is org.springframework.web.client.HttpClientErrorException: 406 null
at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:409) ~[spring-integration-http-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109) ~[spring-integration-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127) ~[spring-integration-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116) ~[spring-integration-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:148) ~[spring-integration-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121) ~[spring-integration-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:89) ~[spring-integration-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:425) ~[spring-integration-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:375) ~[spring-integration-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at com.bhargo.Main.run(Main.java:51) [classes/:na]

我哪里做错了???

如果你说的是JSON,那么考虑添加一个合适的转换器,但不仅如此:

httpRequestExecutingMessageHandler.setMessageConverters(Arrays.asList(new SerializingHttpMessageConverter()

或者,老实说,不要覆盖默认设置。只需使用适当的 content-type header.