0

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"}

Sam
  • 43
  • 5

1 Answers1

1

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.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thanks Artem, Can u please help how can i send custom http headers (key-value) in same request? – Sam Aug 02 '23 at 15:22
  • You need to supply them in the request message upfront, then configure a `HttpRequestExecutingMessageHandler.setHeaderMapper()` to map them into an HTTP request. See more in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping – Artem Bilan Aug 02 '23 at 15:57
  • It would be helpful if you can share a annotation based snippet. – Sam Aug 02 '23 at 16:38
  • HeaderMapper headerMapper = new DefaultHttpHeaderMapper(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Header-1", "Value-1"); httpHeaders.add("Header-2", "Value-2"); headerMapper.toHeaders(httpHeaders); handler.setHeaderMapper(headerMapper); I added this, but doesn't work – Sam Aug 02 '23 at 16:43
  • I'm not sure what you show. See the docs: you nee to provide a `DefaultHttpHeaderMapper` with `thing1, thing2, HTTP_REQUEST_HEADERS` header patterns into your `HttpRequestExecutingMessageHandler` definition. The message you send to the `httpOutRequest` must have those custom headers. The `headerMapper.toHeaders()` is called by the `HttpRequestExecutingMessageHandler`. However that is still wrong. The`fromHeaders()` is used when we perform an HTTP request. – Artem Bilan Aug 02 '23 at 17:18
  • Why do you talk about an "annotation snippet" since you have a plain Java configuration: `new HttpRequestExecutingMessageHandler()`. That's why I suggest you that `setHeaderMapper()`. You probably also miss the fact that there is a configuration phase for those components and runtime when the framework reacts to messages sent to the service activator input channel. – Artem Bilan Aug 02 '23 at 17:20