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 uriVariables = new HashMap();
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 uriVariables = new HashMap();
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 getForObject(String url, Class responseType, Object... uriVariables) throws RestClientException;
T getForObject(String url, Class responseType, Map uriVariables) throws RestClientException;
T getForObject(URI url, Class responseType) throws RestClientException;
ResponseEntity getForEntity(String url, Class responseType, Object... uriVariables) throws RestClientException;
ResponseEntity getForEntity(String url, Class responseType, Map uriVariables) throws RestClientException;
ResponseEntity getForEntity(URI url, Class responseType) throws RestClientException;
1:带参数的POST请求
带参数的URL示例:http://api.map.baidu.com/geodata/v3/poi/create
//正确使用:
HttpHeaders headers = new HttpHeaders();
MultiValueMap 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> requestEntity = new HttpEntity<>(createPostParams, headers);
ResponseEntity responseEntity = restTemplate.postForEntity(PositionConstants.CREATE_URL, requestEntity, String.class);
2:Spring提供的POST方法
T postForObject(String url, Object request, Class responseType, Object... uriVariables)
throws RestClientException;
T postForObject(String url, Object request, Class responseType, Map uriVariables)
throws RestClientException;
T postForObject(URI url, Object request, Class responseType) throws RestClientException;
ResponseEntity postForEntity(String url, Object request, Class responseType, Object... uriVariables)
throws RestClientException;
` ResponseEntity postForEntity(String url, Object request, Class responseType, Map uriVariables)
throws RestClientException;`
ResponseEntity postForEntity(URI url, Object request, Class responseType) throws RestClientException;`
PUT请求和POST请求差不多.
2:Spring提供的PUT方法
void put(String url, Object request, Object... uriVariables) throws RestClientException;
void put(String url, Object request, Map uriVariables) throws RestClientException;
void put(URI url, Object request) throws RestClientException;
1:Spring提供的DELETE方法
void delete(String url, Object... uriVariables) throws RestClientException;
void delete(String url, Map uriVariables) throws RestClientException;
void delete(URI url) throws RestClientException;