RestTemplate常用的幾種請求方式

1、get

/**
         * get請求--exchange方式
         */
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080")
                .queryParam("name", "張三")
                .queryParam("sex", "男")
                .queryParam("national", "中國")
                .queryParam("birthday", "199002190");

        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

        HttpEntity<?> entity = new HttpEntity<>(headers);

        ResponseEntity<String> exchange = restTemplate.exchange(
                builder.build().encode().toUri(),
                HttpMethod.GET,
                entity,
                String.class);

        /**
         * get請求--getForEntity
         */
        ResponseEntity<String> forEntity = restTemplate.getForEntity(builder.build().encode().toString(), String.class, entity);

2、form表單提交--application/x-www-form-urlencoded

RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        map.add("name", "zhangsan");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity( "http://localhost:8080", request , String.class );

3、form表單提交--multipart/form-data

RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<String, Object>();
        InputStream inputStream = multipartFile.getInputStream();
        InputStreamResource inputStreamResource = new InputStreamResource(inputStream){
            @Override
            public long contentLength() throws IOException {
                return multipartFile.getSize();
            }

            @Override
            public String getFilename() {
                return multipartFile.getOriginalFilename();
            }
        };
        map.add("file",inputStreamResource);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(map, headers);

        ResponseEntity<String> response = restTemplate.postForEntity( "http://localhost:8080", request , String.class );

4、application/json

RestTemplate restTemplate = new RestTemplate();
        //請求頭
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //請求體
        Map<String, Object> requestParam = new HashMap<>();
        requestParam.put("name","lisi");
        //封裝成一個請求對象
        HttpEntity entity = new HttpEntity(requestParam, headers);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("http://localhost:8080", entity, String.class);

 

 

 

 

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