找不到合适的HttpMessageConverter请求类型[request.model.RTPRequest]和内容类型[application / x-java-serialized-object]

时间:2018-04-27 14:41:50

标签: spring spring-integration spring-rest

我正在尝试使用Spring Integration的HttpRequestExecutingMessageHandler在REST服务上调用POST方法。

我已经尝试了Could not write request: no suitable HttpMessageConverter found for request type and content type [application/x-java-serialized-object]

上提供的解决方案

以下是我的配置:我在bean配置类上有@EnableIntegration和@IntegrationComponentScan注释。我已经验证我在类路径上有Jackson数据绑定jar。我可以使用POSTMAN获得有效的响应。

@Bean
public MessageChannel rtpRequestChannel() {
    MessageChannel rtpRequestPostOperationRequestChannel = new DirectChannel();
    return rtpRequestPostOperationRequestChannel;
}

@Bean
public MessageChannel rtpResponseChannel() {
    MessageChannel rtpRequestPostOperationResponseChannel = new DirectChannel();
    return rtpRequestPostOperationResponseChannel;
}

@ServiceActivator(inputChannel = "rtpRequestChannel")
@Bean
public MessageHandler httResponseMessageHandler(MessageChannel rtpResponseChannel) {

    HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler(
            "http://myhost:8080/services/v1-0/request");
    handler.setHttpMethod(HttpMethod.POST);
    handler.setOutputChannel(rtpResponseChannel);
    handler.setExpectedResponseType(RTPResponse.class);
    return handler;
}

以下是服务激活器端点的POJO。

@Service
public class RTPRequestServiceClient implements IRTPRequestServiceClient {

    @Override
    @ServiceActivator(inputChannel="rtpRequestChannel")
    public void makeCall(Message<RtpResponse> pr) {
        RtpResponse response = pr.getPayload();
        System.out.println(response);
    }

我的请求POJO是:

public class RTPRequest implements Serializable {

    private static final long serialVersionUID = 567078627620810886L;

    private final String id;
    private final String paymentAmountCcy;
    private final String paymentAmount;

    @JsonCreator
    public RTPRequest(@JsonProperty("id") String id,
            @JsonProperty("paymentAmountCcy") String paymentAmountCcy,
            @JsonProperty("paymentAmount") String paymentAmount) {
        this.id = id;
        this.paymentAmountCcy = paymentAmountCcy;
        this.paymentAmount = paymentAmount;
    }

    // getters ommited for brevity

}

我的回应POJO是:

public class RTPResponse implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -3504586520124861349L;

    private String id;
    private String responseId;

    @JsonCreator
    public RTPResponse(@JsonProperty("id") String id, @JsonProperty("responseId") String responseId) {
        super();
        this.id = id;
        this.responseId = responseId;
    }

    // getters ommited for brevity

}

我发送的请求如下:

RTPRequest body = new RTPRequest(....);

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(IntegrationBeanConfiguration.class);
        MessageChannel rtpRequestChannel = (MessageChannel) context.getBean("rtpRequestChannel");
        rtpRequestChannel
        .send(
                MessageBuilder.withPayload(body)
                .setHeader("accept","application/json")
                .setHeader("Sender","API_GATEWAY")
                .setHeader("ClientId","DIGITAL")
                .build()
            );

我对Spring集成http相当新。我假设RTPRequest对象将通过HttpRequestExecutingMessageHandler默认消息转换器链和类路径中的Jackson数据绑定转换为JSON。

请告知。

1 个答案:

答案 0 :(得分:4)

当您发送请求时,您必须在DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; nameText.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString(); lnameText.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString(); genderText.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString(); telephoneText.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString(); datecreaText.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString(); emailText.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString(); var data = (Byte[])(row.Cells[6].Value); var stream = new MemoryStream(data); pictureBox1.Image = Image.FromStream(stream); 旁边明确设置contentType标题:

accept

强制.setHeader(MessageHeaders.CONTENT_TYPE,"application/json") 使用上述RestTemplate

或者您可以配置MappingJackson2HttpMessageConverter仅使用此HttpRequestExecutingMessageHandler