Spring中RestTemplate的使用

Get請求

1:帶參數的Get請求

請求URL示例:http://localhost:8080/test/sendSms?phone=手機號&msg=短信內容

//錯誤使用:

@Autowired
private RestOperations restOperations;

public void test() throws Exception{
    String url = "http://localhost:8080/test/sendSms";

    Map<String, Object> uriVariables = new HashMap<String, Object>();
    uriVariables.put("phone", "151xxxxxxxx");
    uriVariables.put("msg", "測試短信內容");

    String result = restOperations.getForObject(url, String.class, uriVariables);
}

**服務器接收的時候你會發現,接收的該請求時沒有參數的**
//正確使用:
public void test() throws Exception{
    String url = "http://localhost:8080/test/sendSms?phone={phone}&msg={phone}";

    Map<String, Object> uriVariables = new HashMap<String, Object>();
    uriVariables.put("phone", "151xxxxxxxx");
    uriVariables.put("msg", "測試短信內容");

    String result = restOperations.getForObject(url, String.class, uriVariables);
}

public void test() throws Exception{
    String url = "http://localhost:8080/test/sendSms?phone={phone}&msg={phone}";
    String result = restOperations.getForObject(url, String.class,  "151xxxxxxxx", "測試短信內容");
}

2:Spring提供的Get請求方法

<T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException;

<T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException;

<T> T getForObject(URI url, Class<T> responseType) throws RestClientException;

<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException;

<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException;

<T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException;

Post請求

1:帶參數的POST請求

帶參數的URL示例:http://api.map.baidu.com/geodata/v3/poi/create

//正確使用:

        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> createPostParams = new LinkedMultiValueMap<>(16);
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        createPostParams.add("ak", PositionConstants.AK);
        createPostParams.add("geotable_id", PositionConstants.GEOTABLE_ID);
        createPostParams.add("coord_type", PositionConstants.COORD_TYPE);
        createPostParams.add("latitude", String.valueOf(article.getPositionX()));
        createPostParams.add("longitude", String.valueOf(article.getPositionY()));
        createPostParams.add("address", article.getPositionName());
        createPostParams.add("title", article.getArticleName());
        createPostParams.add("article_img", articleImg);
        createPostParams.add("article_id", article.getArticleId());
        createPostParams.add("article_title", article.getArticleName());
        createPostParams.add("article_time", String.valueOf(article.getArticleTime()));
        createPostParams.add("article_username", userName);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(createPostParams, headers);


        ResponseEntity<String> responseEntity = restTemplate.postForEntity(PositionConstants.CREATE_URL, requestEntity, String.class);

2:Spring提供的POST方法

<T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
			throws RestClientException;

<T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
			throws RestClientException;

<T> T postForObject(URI url, Object request, Class<T> responseType) throws RestClientException;

<T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables)
			throws RestClientException;

`<T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
			throws RestClientException;`
<T> ResponseEntity<T> postForEntity(URI url, Object request, Class<T> responseType) throws RestClientException;`

PUT請求:

PUT請求和POST請求差不多.

2:Spring提供的PUT方法

void put(String url, Object request, Object... uriVariables) throws RestClientException;

void put(String url, Object request, Map<String, ?> uriVariables) throws RestClientException;

void put(URI url, Object request) throws RestClientException;

DELETE請求:

1:Spring提供的DELETE方法

void delete(String url, Object... uriVariables) throws RestClientException;

void delete(String url, Map<String, ?> uriVariables) throws RestClientException;

void delete(URI url) throws RestClientException;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章