本文主要是介绍黑豹程序员-使用RestTemplate模板对象进行访问,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需求
如何直接java程序直接请求另外一个web网址
使用RestTemplate模板对象进行访问
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);}
日志
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":[...] } }
这篇关于黑豹程序员-使用RestTemplate模板对象进行访问的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!