需求
如何直接java程序直接请求另外一个web网址
使用RestTemplate模板对象进行访问
bash
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:6080/risk/center/find?pageNum=1&pageSize=50";
// 发送GET请求
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
// 获取响应数据
String responseBody = response.getBody();
// 打印响应结果
System.out.println("Response body: " + responseBody);
}
日志
bash
11:55:44.298 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET http://localhost:6080/risk/center/find?pageNum=1&pageSize=50
11:55:44.322 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/x-jackson-smile, application/cbor, application/*+json, */*]
11:55:44.466 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
11:55:44.468 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "application/json"
Response body: {"code":200,"msg":"接口调用成功","data":{"records":[...] } }