找不到适合请求类型 [request.model.RTPRequest] 和内容类型 [application/x-java-serialized-object] 的 HttpMessageConverter
no suitable HttpMessageConverter found for request type [request.model.RTPRequest] and content type [application/x-java-serialized-object]
我正在尝试使用 Spring Integration 的 HttpRequestExecutingMessageHandler 在 REST 服务上调用 POST 方法。
我已经尝试过
上提供的解决方案
下面是我的配置:我在 bean 配置上有 @EnableIntegration 和 @IntegrationComponentScan 注释 class。我已经验证我在 class 路径上有 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 默认消息转换器链和 class 路径中的 Jackson 数据绑定转换为 JSON。
请指教
发送请求时,必须明确设置 contentType
header 以及 accept
:
.setHeader(MessageHeaders.CONTENT_TYPE,"application/json")
强制RestTemplate
使用提到的MappingJackson2HttpMessageConverter
。
或者您可以配置 HttpRequestExecutingMessageHandler
仅使用此 MappingJackson2HttpMessageConverter
。
我正在尝试使用 Spring Integration 的 HttpRequestExecutingMessageHandler 在 REST 服务上调用 POST 方法。
我已经尝试过
下面是我的配置:我在 bean 配置上有 @EnableIntegration 和 @IntegrationComponentScan 注释 class。我已经验证我在 class 路径上有 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 默认消息转换器链和 class 路径中的 Jackson 数据绑定转换为 JSON。
请指教
发送请求时,必须明确设置 contentType
header 以及 accept
:
.setHeader(MessageHeaders.CONTENT_TYPE,"application/json")
强制RestTemplate
使用提到的MappingJackson2HttpMessageConverter
。
或者您可以配置 HttpRequestExecutingMessageHandler
仅使用此 MappingJackson2HttpMessageConverter
。