I have created a Rest API that takes a request and gives response to the client. But My API is dependent on a third party service, that instead of giving a response, give a callback to my other endpoint. In order to send the service I need to wait for the callback to be received. How can I achieve it?
My Rest API that needs to send the response.
@POST
// @Produces(MediaType.TEXT_XML)
// @Consumes(MediaType.TEXT_XML)
public ConnectResponse connectAPI(String connectString, @Context HttpHeaders headers) {
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
for (Entry<String, List<String>> entry : headers.getRequestHeaders().entrySet()) {
logger.info("Key = " + entry.getKey() + ", Value = " + entry.getValue());
for (String eachEntry : entry.getValue()) {
logger.info("eachEntry " + eachEntry);
}
}
logger.info("USSD received " + connectString);
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
logger.info("---------- in connect post request ----------------------");
ConnectRequest requestObj = new ConnectRequest();
try {
if (connectString != null && connectString.startsWith("<")) {
requestObj = marshallConnectRequest(connectString);
} else {
requestObj = convertKeyValueToObject(connectString);
}
logger.info("Request is " + requestObj);
} catch (JAXBException e) {
logger.error("----------------- Error in UnMarshalling ----------");
logger.error("----------------- Error in UnMarshalling ----------");
logger.error("----------------- Error in UnMarshalling ----------");
logger.error("----------------- Error in UnMarshalling ----------");
logger.error("----------------- Error in UnMarshalling ----------");
logger.error("----------------- Error in UnMarshalling ----------");
logger.error("----------------- Error in UnMarshalling ----------");
logger.error("----------------- Error in UnMarshalling ----------");
logger.error(e.getMessage(), e);
}
ConnectResponse connectResponse = new ConnectResponse();
connectResponse.setSession(requestObj.getSessionid());
connectResponse.setText("Hello");
logger.info("---------- returning response ----------------------");
logger.info("---------- returning response ----------------------");
logger.info("---------- returning response ----------------------");
logger.info("---------- returning response ----------------------");
logger.info("---------- returning response ----------------------");
logger.info("---------- returning response ----------------------");
logger.info("---------- returning response ----------------------");
logger.info("---------- returning response ----------------------");
return connectResponse;
}
public ConnectRequest marshallConnectRequest(String connectString) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(ConnectRequest.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (ConnectRequest) jaxbUnmarshaller.unmarshal(new StringReader(connectString));
}
public ConnectRequest convertKeyValueToObject(String connectString) {
return new ConnectRequest();
}
Instead of sending a simple response object I want to wait for the callback to hit at the following API.
@Path("/rest")
public class RESTWebservice {
/*
* @Context private MessageContext messageContext;
*/
final Logger logger = Logger.getLogger(RESTWebservice.class);
@POST
@Path("/sendResponse")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public ResponseJSON postQuestionnaire(RequestJSON requestJson) {
// performing operations
}
Call flow : -
- Client will call connectAPI which will call a third party API.
- The third party API after processing above call will callback postQuestionnaire.
- The connectAPI will send response only when the callback at postQuestionnaire is received.
question from:
https://stackoverflow.com/questions/65914313/how-to-send-asynchronous-http-response 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…