Reputation: 352
I am trying to invoke a POST method on a REST service using Spring Integration's HttpRequestExecutingMessageHandler.
I have already tried the solution provided on the Could not write request: no suitable HttpMessageConverter found for request type and content type [application/x-java-serialized-object]
Below is my configuration: I have @EnableIntegration and @IntegrationComponentScan annotations on the bean configuration class. I have verified that I have Jackson data-bind jar on the classpath. I am able to get a valid response using 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;
}
Below is the POJO for service activator endpoint.
@Service
public class RTPRequestServiceClient implements IRTPRequestServiceClient {
@Override
@ServiceActivator(inputChannel="rtpRequestChannel")
public void makeCall(Message<RtpResponse> pr) {
RtpResponse response = pr.getPayload();
System.out.println(response);
}
My request POJO is:
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
}
My responsePOJO is:
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
}
I am sending the request as follows:
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()
);
I am fairly new to Spring integration http. I was assuming that RTPRequest object will be converted into JSON by the virtue of HttpRequestExecutingMessageHandler default message converter chain and Jackson data-bind in classpath.
Please advise.
Upvotes: 2
Views: 9956
Reputation: 121550
When you send your request, you have to explicitly set contentType
header alongside with that accept
:
.setHeader(MessageHeaders.CONTENT_TYPE,"application/json")
To enforce RestTemplate
to use the mentioned MappingJackson2HttpMessageConverter
.
Or you can configure HttpRequestExecutingMessageHandler
only use this MappingJackson2HttpMessageConverter
.
Upvotes: 6