spring 使用restTemplate 实现rest post 提交

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27603235/article/details/79608519

spring 提供了封装rest 接口操作的工具类 RestTemplate,其中,使用POST 提交时,可以使用exchange函数来实现。下面给出实现代码:

HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
                // 第一个参数为请求Body,第二个参数为请求头信息
                MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
                paramMap.add("mobileNo", mobileInfoDTO.getMobile());
                paramMap.add("identifyCode", mobileInfoDTO.getCheckCode());
                HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap);
                ResponseEntity<String> responseEntity = restTemplate.exchange(countUrl, HttpMethod.POST, httpEntity, String.class);
                String response = responseEntity.getBody();  

猜你喜欢

转载自blog.csdn.net/qq_27603235/article/details/79608519