我想使用 Spring
以编程方式从 java 类中调用 GET/POST 方法。我以前在 Servlet 类中做过这些事情,但我不清楚如何用 spring 来做到这一点。我浏览了一些相关教程,但仍然没有弄清楚。任何人都可以解释一下如何做到这一点吗?
谢谢。
最佳答案
由于您正在开发基于 Spring 的应用程序,我建议使用 Spring RestTemplate
请求您的GET/POST端点。
以下可能是可以完成的操作的一小段,您可以引用此 Spring 教程( 1 、 2 和 3 )了解更多详细信息:
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);
}
关于java - 如何使用 spring 以编程方式调用 GET/POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26210993/