当前位置:   article > 正文

springBoot使用 RestTemplate中get 和 post方式传递json参数的多种方法(各种情况都包含)_resttemplate post请求json参数

resttemplate post请求json参数

项目中使用restTemplate的时候总是容易找不到适配的方式  先综合各种使用场景整理如下:

方式一:post---请求json数据-携带请求头

  1. /**
  2. * post请求json数据(添加请求头)
  3. */
  4. public static void test1(){
  5. RestTemplate restTemplate = new RestTemplate();
  6. //创建请求头
  7. HttpHeaders headers = new HttpHeaders();
  8. headers.setContentType(MediaType.APPLICATION_JSON);
  9. String url = "http://localhost:8087/callBackFor";
  10. User student = new User("sansan",10);
  11. HttpEntity<User> entity = new HttpEntity<User>(student, headers);
  12. ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
  13. String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
  14. System.out.println(user);
  15. }

方式二:post --json数据(添加请求头)并传递表单参数

  1. /**
  2. * post请求json数据(添加请求头)并传递表单参数
  3. */
  4. public static void test2(){
  5. RestTemplate restTemplate = new RestTemplate();
  6. //创建请求头
  7. HttpHeaders headers = new HttpHeaders();
  8. headers.setContentType(MediaType.APPLICATION_JSON);
  9. String url = "http://localhost:8087/callBackFor?id={id}";
  10. Map<String, String> params = new HashMap<String, String>();
  11. params.put("id", "123");
  12. User student = new User("sansan",10);
  13. HttpEntity<User> entity = new HttpEntity<User>(student, headers);
  14. ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class, params);
  15. String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
  16. System.out.println(user);
  17. }
  18. /**
  19. * 上述方式的简化版本
  20. */
  21. String template = baseUrl + "/demo?app={0}&userId={1}";
  22. String url = MessageFormat.format(template,app,userId);
  23. return restTemplate.postForEntity(url,null,String.class);

方式三:post --请求携带cookie

  1. HttpHeaders headers = new HttpHeaders();
  2. List<String> cookies = new ArrayList<>();
  3. cookies.add("JSESSIONID=" + Strings.nullToEmpty(jsessionId));
  4. cookies.add("token=" + Strings.nullToEmpty(token));
  5. headers.put(HttpHeaders.COOKIE,cookies);
  6. HttpEntity request = new HttpEntity(null, headers);
  7. ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

方式四:post --表单提交

  1. HttpHeaders headers = new HttpHeaders();
  2. headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  3. MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
  4. map.add("title", title);
  5. map.add("desc", desc);
  6. map.add("userid", toUserId);
  7. HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
  8. ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

方式五:restTemplate.exchange post请求json数据(添加请求头)并传递表单参数

  1. /**
  2. * restTemplate.exchange post请求json数据(添加请求头)并传递表单参数
  3. * exchange可以发送HttpMethod.POST,DELE,GET,PUT请求;
  4. */
  5. public static void test3(){
  6. RestTemplate restTemplate = new RestTemplate();
  7. //创建请求头
  8. HttpHeaders headers = new HttpHeaders();
  9. headers.setContentType(MediaType.APPLICATION_JSON);
  10. String url = "http://localhost:8087/callBackFor?id={id}";
  11. //params是url中出现的路径变量
  12. Map<String, String> params = new HashMap<String, String>();
  13. params.put("id", "123");
  14. User student = new User("sansan",10);
  15. //entity包含请求的对象和消息头;
  16. HttpEntity<User> entity = new HttpEntity<User>(student, headers);
  17. ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class, params);
  18. String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
  19. System.out.println(user);
  20. }

方式六:get方式请求图片

  1. HttpHeaders headers = new HttpHeaders();
  2. headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
  3. HttpEntity<String> entity = new HttpEntity<String>(headers);
  4. ResponseEntity<byte[]> response = restTemplate.exchange(url,HttpMethod.GET, entity, byte[].class);
  5. byte[] imageBytes = response.getBody();

方式七:post请求json数据(添加请求头) 直接传递jsonString数据

  1. /**
  2. * post请求json数据(添加请求头) 直接传递jsonString数据
  3. */
  4. public static void test4(){
  5. RestTemplate restTemplate = new RestTemplate();
  6. //创建请求头
  7. HttpHeaders headers = new HttpHeaders();
  8. headers.setContentType(MediaType.APPLICATION_JSON);
  9. //也可以这样设置contentType
  10. //MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  11. //headers.setContentType(type);
  12. //加不加Accept都可以
  13. //headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  14. String url = "http://localhost:8087/callBackFor";
  15. User student = new User("sansan",19);
  16. String jsonString = JSONObject.toJSONString(student);
  17. System.out.println(jsonString);//{"age":10,"name":"sansan"}
  18. HttpEntity<String> entity = new HttpEntity<String>(jsonString, headers);
  19. ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
  20. String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
  21. System.out.println(user);
  22. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/303040
推荐阅读
相关标签
  

闽ICP备14008679号