Sudip Bolakhe :
I am working on micro-service architecture. I have 3 different application with a different domain name. Suppose application 1 has an API:
"/app/foo-bar"
This API is called from some service of application 2, the service is also triggered by some API. This API can also be called from application 3.
What I need now is, when the API is called in application 1, I want to find out from which application it was called. ( domain name of application 2 or application 3)
I tried this :
request.getHeader("referer");
But this returns me null
Is there any way to solve this?
Md. Sajedul Karim :
You have to set header during call to the API.Also you have to get header in that API for origin of caller.
SET header during API call: app/foo-bar
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("referer", "APP_1");
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
Get Header In API
@RequestMapping(value = "/app/foo-bar")
public String serveRest(@RequestBody String body, @RequestHeader HttpHeaders headers) {
List<String> parameters = headers.get("parameters");
String referer = null;
for (String header : parameters) {
if (header.equals("referer")) {
referer = header;
}
}
System.out.println(referer);
return referer;
}
Here is the imports:
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;