简体   繁体   English

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

[英]spring integration 406 null when passing a custom object to HttpRequestExecutingMessageHandler

I am using HttpRequestExecutingMessageHandler to make a POST request to a local service. 我正在使用HttpRequestExecutingMessageHandler向本地服务发出POST请求。

The service is as follows :- 服务如下:

@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();
    }
}

The User class is :- 用户类是:

public class User implements Serializable {

    private String name;
    private Integer age;

//getters and setters
}

The spring integration config is : 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();
    }

In the main class I send a message to the httpReqChannel channel as follows:- 在主类中,我将消息发送到httpReqChannel通道,如下所示:

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

I get the following exception:- 我收到以下异常:

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]

What am I doing wrong here ??? 我在这里做错了什么?

If you talk about JSON, then consider to add an appropriate converter, but not only this: 如果您谈论JSON,请考虑添加适当的转换器,但不仅限于此:

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

Or, honestly, just don't override default set. 或者说实话,就是不要覆盖默认设置。 Just use an appropriate content-type header. 只需使用适当的content-type标题即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM