import java.util.HashMap; import java.util.Map; import org.springframework.web.client.RestTemplate; /** * @author administrator * * RestTemplate provides a series of interfaces for calling spring mvc rest (or spring rest webservice) * including get/post/delete/put/ * */ public class Resttemplate { /** * @param args */ public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); //get method************************************************ **************************************************** ************ // //The parameter is placed directly in the URL // String message = restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80", String.class ); // // // //Parameters are passed using MAP // Map<String ,Object> urlVariables = new HashMap<String ,Object>(); // urlVariables.put("name", "zhaoshijie"); // urlVariables.put("id", 80); // String message2 = restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate", String.class, urlVariables); //delete method********************************************** **************************************************** ************ //delete method (Note: the delete method has no return value, indicating that the parameter id=0 can not be defined on the server side, and can be obtained directly by request) // restTemplate.delete("http://localhost:8080/yongbarservice/appstore/appgoods/deleteranking?id=0"); //post method************************************************ **************************************************** ************ //Use MAP to pass parameters // Map<String ,Object> urlVariables = new HashMap<String ,Object>(); // urlVariables.put("name", "zhaoshijie"); // urlVariables.put("id", 80); // String message3 = restTemplate.postForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate",null, String.class, urlVariables); // pass parameters directly using the URL // String message = restTemplate.postForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80",null, String.class ); //put method************************************************ **************************************************** ************ //Note: The delete method has no return value, indicating that the parameter of id=0 can not be defined on the server side, and can be obtained directly by request restTemplate.put("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80" ,null); // System.out.println(message); // System.out.println(message2); // System.out.println(message3); } }
http://zhaoshijie.iteye.com/blog/1711170
http://yangjizhong.iteye.com/blog/600540