2017-12-07 8 views
-1

私は、サーバー1上でサービス注釈付きのクラスを持っています。パラメータで春のリモートメソッドを呼び出す

@Service 
public class MainHandler implements AbstractHandler { 

    @Autowired 
    private ServiceLocal defaultService; 

    @Override 
    public boolean execute(HttpServletRequest request, HttpServletResponse response) throws MsisdnServiceException { 
      System.out.println("The default Request" + request); 
    } 
} 

私は春に行う方法が何であるか、リクエストを通過した後、他のリモートサーバからこのメソッドを呼び出すと、このからの応答を取得したいです。

+0

downvotingリサーチが行われていないため、難しい問題ではなく、リモートメソッドとHTTPを混乱させるためです。 – tkruse

答えて

1

リモートでメソッドを呼び出すと、簡単にGoogleを実行できるRMIという技術が使用されます。

しかし、HttpServletRequestとHttpServletResponseを使いたいので、おそらくSpring MVCを使ってHttpコントローラを書くべきです。そのためにも、Googleを利用して、優れたチュートリアルやガイドを簡単に見つけることができます。

0

スプリングのRestTemplateを使用してサーバーと通信できます。

あなたはserver 2からデータを取得するためにserver 1バックエンド上のコントローラを作成する必要があります:まず

void request() { 
    String url = "http://localhost:8080/endpoint"; 

    HttpHeaders httpHeaders = new HttpHeaders(); 
    httpHeaders.set("Content-Type", "application/json"); 

    JSONObject json = new JSONObject(); 
    json.put("name", "yourName"); 
    json.put("email", "[email protected]"); 

    HttpEntity <String> httpEntity = new HttpEntity <String> (json.toString(), httpHeaders); 

    RestTemplate restTemplate = new RestTemplate(); 
    String response = restTemplate.postForObject(url, httpEntity, String.class); 
} 

@RestController 
public class MyController { 
    @RequestMapping(value = "/endpoint", method = RequestMethod.POST) 
    String execute(@RequestBody MyClass object) { 
      System.out.println("Your data" + object); 
     } 
    } 

server 2バックエンドではRestTemplateserver 1のエンドポイントへのREST呼び出しを行うメソッドを作成します