I want to pass json parameters in the URL of rest API with SI - HttpRequestExecutingMessageHandler.
This is the Controller ---
@RestController
@RequestMapping("/api")
public class NotificationController {
@Autowired
NotificationGateway notificationGateway;
@PostMapping
@RequestMapping("/sendmsg")
public String sendMsg(@RequestBody SMSData smsData) {
return notificationGateway.sendMsg(smsData);
}
}
This is the Gateway ---
@MessagingGateway
public interface NotificationGateway {
@Gateway(requestChannel = "httpOutRequest")
public String sendMsg(SMSData smsData);
}
This is the Service ---
@Configuration
public class HTTPConfig {
private final Logger log = LoggerFactory.getLogger(HTTPConfig.class);
@Bean
@ServiceActivator(inputChannel = "httpOutRequest")
public MessageHandler outbound() {
log.info("Inside HTTPConfig :: outbound() invoked ");
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler(
"http://localhost:9090/Gateway/rest?msg={msg}&mobileNo={mobileNo}", restTemplate());
handler.setHttpMethod(HttpMethod.GET);
handler.setExpectedResponseType(String.class);
handler.setExpectReply(true);
ExpressionParser expressionParser = new SpelExpressionParser();
handler.setUriVariableExpressions(
Collections.singletonMap("msg", expressionParser.parseExpression("headers['msg']")));
handler.setUriVariableExpressions(
Collections.singletonMap("mobileNo", expressionParser.parseExpression("headers['mobileNo']")));
return handler;
}
@Bean
public RestTemplateBuilder restTemplateBuilder() {
log.info("Inside HTTPConfig :: restTemplateBuilder() invoked ");
return new RestTemplateBuilder();
}
@Bean
public RestTemplate restTemplate() {
log.info("Inside HTTPConfig :: restTemplate() invoked ");
RestTemplate restTemplate = new RestTemplate();
return restTemplate;
}
}
This is the DTO---
public class SMSData {
public String msg;
public String mobileNo;
}
Please help with how to pass parameters in outbound() JSON --- {"msg" : "This is my message", "mobileNo" : "00123456987"}
Your contract is this public String sendMsg(SMSData smsData);
, but then you try to pass into request params from headers: headers['msg']
and headers['mobileNo']
.
If you say that your SMSData
contains all the info, then you need to make those expressions against payload: payload['msg']
and payload['mobileNo']
, respectively.