spring integration 406 null when passing a custom

2019-03-04 05:05发布

问题:

I am using HttpRequestExecutingMessageHandler to make a POST request to a local service.

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 :

@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:-

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 ???

回答1:

If you talk about JSON, then consider to add an appropriate converter, but not only this:

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

Or, honestly, just don’t override default set. Just use an appropriate content-type header.