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);

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值