Spring RestTemplate 之put、delete請求


●PUT請求:在RestTemplate中,PUT請求可以通過put方法調用,put方法的參數和前面介紹的postForEntity方法的參數基本一致,只是put方法沒有返回值而已。舉一個簡單的例子,如下:(缺點:沒有返回值,不能得到接口的返回)

 

@RequestMapping("/hello7/{flag}")

public String getHello7() throws Exception {

    HttpHeaders header = new HttpHeaders();

    header.setContentType(MediaType.APPLICATION_JSON_UTF8);

    Map<String, Object> m = new HashMap<String, Object>();

    m.put("t1", "xx");

    m.put("flag", "1");

    ObjectMapper mapper = new ObjectMapper();

    String value = mapper.writeValueAsString(m);

    HttpEntity<String> entity = new HttpEntity<String>(value,header);

    restTemplate.put("http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor", entity);

    return "success";

}

●DELETE請求:delete請求我們可以通過delete方法調用來實現,如下例子:

 

@RequestMapping("/delete")

public void delete() {

    restTemplate.delete("http://HELLO-SERVICE/getbook4/{1}", 100);

}

delete方法也有幾個重載的方法,不過重載的參數和前面基本一致


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM