软件系统开发定制SpringBoot使用RestTemplate

是由 Spring 软件系统开发定制提供的一个 HTTP 请求工具,软件系统开发定制它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute。RestTemplate 继承自 InterceptingHttpAccessor 并且实现了 RestOperations 接口,其中 RestOperations 接口定义了基本的 RESTful 操作,这些操作在 RestTemplate 中都得到了实现。

POST请求

postForObject

1、使用LinkedMultiValueMap作为参数(Form表单提交)

  1. RestTemplate template = new RestTemplate();
  2. String url = "http://127.0.0.1:8800/product/update";
  3. MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
  4. paramMap.add("id", "123");
  5. paramMap.add("name", "张三");
  6. String result = template.postForObject(url, paramMap, String.class);
  7. System.out.println("result:" + result);

2、使用Object作为参数(JSON提交)

  1. RestTemplate template = new RestTemplate();
  2. String url = "http://127.0.0.1:8800/product/update";
  3. User user = new User(123, "张三");
  4. String result = template.postForObject(url, user, String.class);
  5. System.out.println("result:" + result);

3、使用JSONObject作为参数(JSON提交)

  1. RestTemplate template = new RestTemplate();
  2. String url = "http://127.0.0.1:8800/product/update";
  3. JSONObject obj = new JSONObject();
  4. obj.put("id", "123");
  5. obj.put("name", "张三");
  6. String result = template.postForObject(url, obj, String.class);
  7. System.out.println("result:" + result);

postForEntity

1、使用LinkedMultiValueMap作为参数(Form表单提交)

  1. RestTemplate template = new RestTemplate();
  2. String url = "http://127.0.0.1:8800/product/update";
  3. HttpHeaders headers = new HttpHeaders();
  4. headers.set("token", "asdf");
  5. MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
  6. paramMap.add("id", "123");
  7. paramMap.add("name", "张三");
  8. HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers);
  9. ResponseEntity<String> response = template.postForEntity(url, httpEntity, String.class);
  10. System.out.println("result:" + response.getBody());

2、使用Object作为参数(JSON提交)

  1. RestTemplate template = new RestTemplate();
  2. String url = "http://127.0.0.1:8800/product/update";
  3. HttpHeaders headers = new HttpHeaders();
  4. User user = new User(123, "张三");
  5. HttpEntity<User> httpEntity = new HttpEntity<User>(user, headers);
  6. ResponseEntity<String> response = template.postForEntity(url, httpEntity, String.class);
  7. System.out.println("result:" + response.getBody());

3、使用JSONObject为参数(JSON提交)

  1. RestTemplate template = new RestTemplate();
  2. String url = "http://127.0.0.1:8800/product/update";
  3. HttpHeaders headers = new HttpHeaders();
  4. JSONObject obj = new JSONObject();
  5. obj.put("id", "123");
  6. obj.put("name", "张三");
  7. HttpEntity<JSONObject> httpEntity = new HttpEntity<JSONObject>(obj, headers);
  8. ResponseEntity<String> response = template.postForEntity(url, httpEntity, String.class);
  9. System.out.println("result:" + response.getBody());

exchange

  1. RestTemplate template = new RestTemplate();
  2. String url = "http://127.0.0.1:8800/product/productDetail";
  3. HttpHeaders headers = new HttpHeaders();
  4. MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
  5. paramMap.add("id", "123");
  6. HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers);
  7. ResponseEntity<String> response = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
  8. System.out.println("result:" + response.getBody());

postForObject和postForEntity方法的区别主要在于可以在postForEntity方法中设置header的属性,当需要指定header的属性值的时候,使用postForEntity方法。

exchange方法和postForEntity类似,但是更灵活,exchange还可以调用get、put、delete请求。

GET请求

getForObject

  1. RestTemplate template = new RestTemplate();
  2. String url = "http://127.0.0.1:8800/product/detail?id={id}";
  3. Map<String, Object> paramMap = new HashMap<String, Object>();
  4. paramMap.put("id", "123");
  5. String result = template.getForObject(url, String.class, paramMap);
  6. System.out.println("result:" + result);

getForEntity 

  1. RestTemplate template = new RestTemplate();
  2. String url = "http://127.0.0.1:8800/product/detail?id={id}";
  3. Map<String, Object> paramMap = new HashMap<String, Object>();
  4. paramMap.put("id", "123");
  5. ResponseEntity<String> response1 = template.getForEntity(url, String.class, paramMap);
  6. System.out.println("result:" + response1.getBody());

exchange

  1. RestTemplate template = new RestTemplate();
  2. String url = "http://127.0.0.1:8800/product/productDetail";
  3. HttpHeaders headers = new HttpHeaders();
  4. headers.set("token", "asdf");
  5. HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null, headers);
  6. ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);
  7. System.out.println("result:" + response.getBody());

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发