我正在尝试使用Spring Integration的httprequestexecutingmessagehandler在REST服务上调用帖子方法。
我已经尝试了无法编写请求的解决方案:找不到适用于请求类型和内容类型的httpmessageconverter [application/x-java-serialized-object]
以下是我的配置:我在bean配置类上有@enable Integration和@integrationComponentscan的注释。我已经证实了我在类路径上有杰克逊数据绑定的罐子。我能够使用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()
);
我是春季集成http的新手。我假设rtprequest对象将通过httprequestexecutingmessagehandler默认消息转换器链和jackson data-bind在classPath中转换为JSON。
请建议。
在发送请求时,必须与该accept
一起明确设置contentType
标头:
.setHeader(MessageHeaders.CONTENT_TYPE,"application/json")
执行RestTemplate
使用上述MappingJackson2HttpMessageConverter
。
或您可以配置HttpRequestExecutingMessageHandler
仅使用此MappingJackson2HttpMessageConverter
。