一、REST即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。
public ResponseEntity doPost(String url, Map<String,Object> map) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); Gson gson = new Gson(); HttpEntity<String> httpEntity = new HttpEntity(gson.toJson(map), httpHeaders); ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Object.class); return responseEntity; }
说明:这种方式主要是用于post数据的传输,因为rest的简洁性,在使用上面也会得到恨到的应用。
2)第二种get请求
public ResponseEntity doGet(String url) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.TEXT_html); HttpEntity<String> httpEntity = new HttpEntity(httpHeaders); ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Object.class); return responseEntity; }
说明:get请求没有太大的解释,基本上面的设置都是这样
3)第三种from方式
public ResponseEntity doFrom(String url, LinkedMultiValueMap<String, Object> map) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<String> httpEntity = new HttpEntity(httpHeaders); ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Object.class); return responseEntity; }
说明:表单的方式使用虽然不常见,但是应用的时候也需要注意几点,数据形式LinkedMultiValueMap和HashMap不同存储方式的是name=rest&password=123。
而Content-Type的方式为application/x-www-form-urlencoded。这种表单处理方式,对于数据的处理上面要特别注意