0

I have 3 servers,serverA,serverB,serverC,Now in the serverC,some request from serverB is by processed,and then,I don't know what is the result(response),if it's resultA,I want give the resultA to the serverA as a request,else give the serverB.

so what I can do something in the serverC's controller,or there is something wrong in the desgin.

Please tell me what I should to do,Thanks.

enter image description here

This is my code.

serverA

@RestController
public class ControllerA {

@RequestMapping(value = "/methodA", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<String> methodA(@RequestBody String something) {
    // some process
    return null;
}

serverB

@RestController
public class ControllerB {

@RequestMapping(value = "/methodB", consumes =MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> methodB(@RequestBody String something) {
    // some process
    return null;
}

serverC

@RestController
public class ControllerC {

public ResponseEntity<String> methodC(@RequestBody String someReq) {
    if (checkPam(someReq)) {
        **// I want to call the ControllerA in serverA.**
    }else {
        **// I want to call the ControllerB in serverB.**
    }
    return null;
}
clemens
  • 14,173
  • 11
  • 38
  • 52
beike
  • 13
  • 1
  • 5
  • Try use `restTemplate` – Hadi J Jan 20 '18 at 09:05
  • Check out the SO response here https://stackoverflow.com/questions/2047122/requestdispatcher-forward-vs-httpservletresponse-sendredirect – Karen12345 Jan 20 '18 at 09:07
  • 1
    by using classical HTTP response is always returned to the server who requests it. In your case also by using the `restTemplate` if serverB requests a resource, the response will always return to it. What you can do is to use some messaging system For example serverB makes a request. ServerC responds to it with http status code 200 (it means OK i accept your request) then it elaborates the request and by the request type it can generate a message (e.g. JMS, websocket etc..) on a topic/queue and the correct listener will intercept the message. Same for the second scenario to serverB – Angelo Immediata Jan 20 '18 at 09:33

1 Answers1

0

You can simply Use RestTemplate:

@RestController
public class ControllerC {

public ResponseEntity<String> methodC(@RequestBody String someReq) {
     RestTemplate restTemplate = new RestTemplate();
    if (checkPam(someReq)) {          
        String fooResourceUrl
           = "http://path-to-server-a/path-to-service-a";
        ResponseEntity<String> response
        = restTemplate.getForEntity(fooResourceUrl , String.class);
    }else {
        String fooResourceUrl
           = "http://path-to-server-b/path-to-service-b";
        ResponseEntity<String> response
        = restTemplate.getForEntity(fooResourceUrl , String.class);
    }
    return null;
}

As you can see, I instantiate RestTemplate object by new operator, you can also declare RestTemplate bean in your context and then autowire it in your controller class.

saeid rastak
  • 186
  • 1
  • 11
  • Thanks for your words,I think it is great.But i get some new problem.In this case,Can I try call post something to methodC,but Mock the result that form serviceA or serviceB. – beike Jan 22 '18 at 13:25
  • Your welcome, please ask a new question and explain your new situations. – saeid rastak Feb 01 '18 at 07:12