为网友们分享了相关的编程文章,网友宿白梦根据主题投稿了本篇教程内容,涉及到Spring RestTemplate、Spring RestTemplate基本使用、Spring RestTemplate相关内容,已被297网友关注,涉猎到的知识点内容可以在下方电子书获得。
Spring RestTemplate
引言
最近的项目需求中有使用到后端发送http请求,在网上寻找资料后发现可以使用spring自带的RestTemplate类实现,故作此记录
项目使用
将下述代码引入spring容器中(RestTemplate是spring自带的发送http请求的类)
@Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory) { RestTemplate restTemplate = new RestTemplate(factory); return restTemplate; } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(5000); factory.setConnectTimeout(15000); // 设置代理 //factory.setProxy(null); return factory; }
在需要使用的类中直接引入即可
@Autowired private RestTemplate restTemplate;
代码示例
这里简单示例下post请求,如果又其它需求的可以去搜索一下其它文章
**注意:**发送的post的传参的类型一定要是LinkedMultiValueMap
(不能使用jsonobject),否则会报找不到数据的错误
@GetMapping("send") public String testPost(@RequestParam("code") Integer code){ String url = "http://localhost:8888/oauth/return"; LinkedMultiValueMap<String, Object> stringObjectLinkedMultiValueMap = new LinkedMultiValueMap<>(); stringObjectLinkedMultiValueMap.set("code",code); HttpEntity<LinkedMultiValueMap<String, Object>> jsonObjectHttpEntity = new HttpEntity<>(stringObjectLinkedMultiValueMap); ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, jsonObjectHttpEntity, String.class); String body = stringResponseEntity.getBody(); return body; } @PostMapping("return") public String returnPost(@RequestParam Integer code){ return "哈首府帕斯发"; }
踩坑记录
发送POST请求传参的问题
如下代码,如果使用jsonobject来装载数据发送post请求是会报错的,只能使用LinkedMultiValueMap
@GetMapping("keycloak/getToken") public String keycloakGetToken(@RequestParam Map<String,String> map){ //String code = requestObj.getString("code"); String url = "http://localhost:8081/auth/realms/myRealm/protocol/openid-connect/token"; String code = map.get("code"); System.out.println("传进来的code:"+code); //HttpHeaders headers = new HttpHeaders(); //JSONObject jsonObject = new JSONObject(); //jsonObject.put("grant_type","authorization_code"); //jsonObject.put("code",code); //jsonObject.put("client_id","myclient"); //jsonObject.put("client_secret","LsvDoZ5g0BQhRDR9ydYqV9tjal8YDeAI"); //jsonObject.put("scope","openid"); //jsonObject.put("redirect_uri","http://localhost:8888/oauth/keycloak/getToken"); //HttpEntity<JSONObject> jsonObjectHttpEntity = new HttpEntity<>(jsonObject,headers); LinkedMultiValueMap<String, Object> stringObjectLinkedMultiValueMap = new LinkedMultiValueMap<>(); stringObjectLinkedMultiValueMap.set("grant_type","authorization_code"); stringObjectLinkedMultiValueMap.set("code",code); stringObjectLinkedMultiValueMap.set("client_id","myclient"); stringObjectLinkedMultiValueMap.set("client_secret","LsvDoZ5g0BQhRDR9ydYqV9tjal8YDeAI"); stringObjectLinkedMultiValueMap.set("scope","openid"); stringObjectLinkedMultiValueMap.set("redirect_uri","http://localhost:8888/oauth/keycloak/getToken"); HttpEntity<LinkedMultiValueMap<String, Object>> jsonObjectHttpEntity = new HttpEntity<>(stringObjectLinkedMultiValueMap); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, jsonObjectHttpEntity, String.class); String body = responseEntity.getBody(); return body; }
到此这篇关于Spring RestTemplate的使用与踩坑的文章就介绍到这了,更多相关Spring RestTemplate内容请搜索码农之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持码农之家!