RestTemplate post请求传参方式

RestTemplate post请求方式主要分为下面几种:

一、postForObject

        RestTemplate template = new RestTemplate();
        String url = "http://localhost/test";
        MultiValueMap paramMap = new LinkedMultiValueMap();
        paramMap.add("name", "xxx");
        Map respnose = template.postForObject(url, paramMap, Map.class);
        Assert.assertNotNull(respnose);

二、postForEntity

        RestTemplate template = new RestTemplate();
        String url = "http://localhost/test";
        MultiValueMap paramMap = new LinkedMultiValueMap();
        paramMap.add("name", "xxx");
        HttpHeaders headers = new HttpHeaders();
        HttpEntity> httpEntity = new HttpEntity>(paramMap,headers);
        ResponseEntity response2 = template.postForEntity(url, httpEntity, Map.class);
        Assert.assertEquals(getStatusCode,200);

三、exchange

        RestTemplate template = new RestTemplate();
        String url = "http://localhost/test";
        MultiValueMap paramMap = new LinkedMultiValueMap();
        paramMap.add("name", "xxx");
        ResponseEntity response3 = template.exchange(url, HttpMethod.POST, httpEntity, Map.class);
        Assert.assertEquals(getStatusCode,200);

你可能感兴趣的:(springcloud)