当前位置:   article > 正文

restTemplate.postForObject传参

resttemplate.postforobject
  1. import com.alibaba.fastjson.JSONObject;
  2. import com.itmuch.cloud.study.user.entity.User;
  3. import com.itmuch.cloud.study.user.util.RestTemplateUtil;
  4. import org.junit.Test;
  5. import org.springframework.http.HttpHeaders;
  6. import org.springframework.web.client.RestTemplate;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. public class ResttemplateApplicationTests {
  10. @Test
  11. public void testJavaObj() {
  12. User request = new User();
  13. request.setAge(18);
  14. request.setName("小芳");
  15. request.setAddress("广东深圳");
  16. RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");
  17. String url = "http://localhost:8011/pojo";
  18. String result = restTemplate.postForObject(url, request, String.class);
  19. System.out.println(result);
  20. }
  21. @Test
  22. public void testMap() {
  23. Map<String, Object> hashMap = new HashMap<String, Object>();
  24. hashMap.put("age", 18);
  25. hashMap.put("name", "小芳");
  26. hashMap.put("address", "广东深圳");
  27. RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");
  28. String url = "http://localhost:8011/map";
  29. String result = restTemplate.postForObject(url, hashMap, String.class);
  30. System.out.println(result);
  31. }
  32. @Test
  33. public void testJson() {
  34. JSONObject json = new JSONObject();
  35. json.put("age", 18);
  36. json.put("name", "小芳");
  37. json.put("address", "广东深圳");
  38. RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");
  39. String url = "http://localhost:8011/json";
  40. String result = restTemplate.postForObject(url, json, String.class);
  41. System.out.println(result);
  42. }
  43. @Test
  44. public void testGet() {
  45. RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");
  46. String url = "http://localhost:8012/testPost?username=刘备&name=CTO&age=18";
  47. HttpHeaders headers = new HttpHeaders();
  48. headers.add("Content-Type", "text/xml; charset=utf-8");
  49. String result= restTemplate.getForObject(url,String.class,headers);
  50. System.out.println(result);
  51. }
  52. @Test
  53. public void testPost() {
  54. RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");
  55. HttpHeaders headers = new HttpHeaders();
  56. headers.add("Content-Type", "text/xml; charset=utf-8");
  57. String url = "http://localhost:8012/testPost?username=刘备&name=CTO&age=18";
  58. String result= restTemplate.postForObject(url,headers,String.class);
  59. System.out.println(result);
  60. }
  61. }
  1. import org.springframework.http.converter.StringHttpMessageConverter;
  2. import org.springframework.web.client.RestTemplate;
  3. import java.nio.charset.Charset;
  4. public class RestTemplateUtil {
  5. /**
  6. * 创建指定字符集的RestTemplate
  7. *
  8. * @param charset
  9. * @return
  10. */
  11. public static RestTemplate getInstance(String charset) {
  12. RestTemplate restTemplate = new RestTemplate();
  13. restTemplate.getMessageConverters().add(new StringHttpMessageConverter(Charset.forName(charset)));
  14. return restTemplate;
  15. }
  16. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/303032
推荐阅读
相关标签
  

闽ICP备14008679号