如何使用spring以编程方式调用GET/POST方法

Sai*_*Sai 0 java spring servlets spring-mvc

我想在java类中使用编程方式调用GET/POST方法Spring.我以前在Servlet类中做过这些东西但是我不清楚如何用spring做这个.我经历了一些相关的教程,但我仍然没有被清除.任何人都可以解释一下如何做到这一点?谢谢.

tma*_*wen 7

由于您正在使用基于Spring的应用程序,我建议使用Spring RestTemplate来请求您的GET/POST端点.

下面可能是有什么可以做了一小段,你可以参考这个春天教程(1,23)了解更多详情:

public void getOrPostTest() {

  String GET_URL = "http://localhost:8080/somepath";

  RestTemplate restTemplate = new RestTemplate();

  Map<String, String> params = new HashMap<String, String>();
  params.put("prop1", "1");
  params.put("prop2", "value");

  String result = restTemplate.getForObject(GET_URL, String.class, params);
}
Run Code Online (Sandbox Code Playgroud)