Spring Restful客户端 - RestTemplate示例
时间:2020-02-23 14:36:00 来源:igfitidea点击:
在本教程中,我们将看到如何使用Spring RestTemplate创建REST客户端。
我们已经看到了Spring Restful Web服务Crud示例。
我们使用邮递实用程序来演示所有HTTP方法,如Get,Post,Delete和Put,但如果要为RESTful客户端编写Java代码,则可以使用Spring RestTemplate。
我们可以始终使用Java的httpclient,但Spring RestTemplate提供了更多方法和选项,可以通过HTTP方法使用来消耗Spring Resful Web服务。
以下是Spring RestTemplate为每个HTTP方法提供的方法列表。
| Method | Spring RestTemplate's method |
|---|---|
| Get | getForObject, getForEntity |
| Post | postForObject(String url, Object request, Class responseType, String… uriVariables)postForLocation(String url, Object request, String… urlVariables), |
| Put | put(String url, Object request, String…urlVariables) |
| Delete | delete() |
| Head | headForHeaders(String url, String… urlVariables) |
| Options | optionsForAllow(String url, String… urlVariables) |
使用get的例子:
我们可以使用GetforoPject或者GetForent来调用HTTP Get方法。
Spring REST API代码GET方法:
@RequestMapping(value = "/country/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public Country getCountryById(@PathVariable int id) {
return countryService.getCountry(id);
}
Spring RestTemplate Get方法:
package org.igi.theitroad.client;
import org.igi.theitroad.bean.Country;
import org.springframework.web.client.RestTemplate;
/**
* @author igi Mandliya
*/
public class SpringRestTemplateExample {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
Country bhutan = restTemplate
.getForObject("http://localhost:8080/SpringRestfulWebServicesCRUDExample/country/{id}", Country.class,2);
System.out.println("Country Name:"+bhutan.getCountryName());
System.out.println("Population:"+bhutan.getPopulation());
}
}
运行上面的代码时,我们将得到以下输出:
Country Name:Bhutan Population:7000
使用post的例子:
我们可以使用PostForObject或者Postforlocation来调用post方法。
Spring REST API代码POST方法:
@RequestMapping(value = "/countries", method = RequestMethod.POST, headers = "Accept=application/json")
public Country addCountry(@RequestBody Country country) {
return countryService.addCountry(country);
}
Spring RestTemplate Post方法:
package org.igi.theitroad.client;
import org.igi.theitroad.bean.Country;
import org.springframework.web.client.RestTemplate;
/**
* @author igi Mandliya
*/
public class SpringRestTemplateExample {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
final String uri = "http://localhost:8080/SpringRestfulWebServicesCRUDExample/countries";
Country country = new Country();
country.setCountryName("USA");
country.setPopulation(4000);
Country addedCountry = restTemplate.postForObject( uri, country, Country.class);
System.out.println("Country added : " +addedCountry.getCountryName());
}
}
运行上面的代码时,我们将得到以下输出:
Country added : USA
说明:
我们可以使用put来调用HTTP PUT方法。
Spring Rest API代码PUT方法:
@RequestMapping(value = "/countries", method = RequestMethod.PUT, headers = "Accept=application/json")
public Country updateCountry(@RequestBody Country country) {
return countryService.updateCountry(country);
}
Spring RestTemplate Put方法:
package org.igi.theitroad.client;
import java.util.HashMap;
import java.util.Map;
import org.igi.theitroad.bean.Country;
import org.springframework.web.client.RestTemplate;
/**
* @author igi Mandliya
*/
public class SpringRestTemplateExample {
public static void main(String args[]) {
final String uriForPut = "http://localhost:8080/SpringRestfulWebServicesCRUDExample/countries";
Country country = new Country();
country.setId(2);
country.setCountryName("Bhutan");
country.setPopulation(10000);
RestTemplate restTemplate = new RestTemplate();
restTemplate.put ( uriForPut, country);
}
}
运行上面的代码时,不丹的人口将更新到10000。
删除示例:
我们可以使用删除来调用HTTP Delete方法。
Spring Rest API代码删除方法:
@RequestMapping(value = "/country/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
public void deleteCountry(@PathVariable("id") int id) {
countryService.deleteCountry(id);
}
Spring RestTemplate Delete方法:
package org.igi.theitroad.client;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.client.RestTemplate;
/**
* @author igi Mandliya
*/
public class SpringRestTemplateExample {
public static void main(String args[]) {
final String uriForDelete = "http://localhost:8080/SpringRestfulWebServicesCRUDExample/country/{id}";
Map<String, String> params = new HashMap<String, String>();
params.put("id","2");
RestTemplate restTemplate = new RestTemplate();
restTemplate.delete ( uriForDelete,params);
}
}

