java-写一个方法用于请求访问使用Resttemplate方法参数为url和参数对象
以下是一个示例方法,使用RestTemplate发送HTTP GET请求,并返回响应体字符串:
import org.springframework.web.client.RestTemplate;
public class RequestUtil {
public static String sendGetRequest(String url, Object params) {
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(url, String.class, params);
return response;
}
}
有关RestTemplate的更多信息,请参阅Spring官方文档:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html
原文地址: https://www.cveoy.top/t/topic/rlN 著作权归作者所有。请勿转载和采集!