当前位置:   article > 正文

resttemplate几种格式传参方式_resttemplate传参

resttemplate传参

1. form-data 格式 :HttpEntity< MultiValueMap<String, Object>>形式传过去

 HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
            // 设置以表单的方式提交
        headers.add("Content-Type",MediaType.MULTIPART_FORM_DATA_VALUE);
        headers.add("token","aaaaaa");
            //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("status","aaa");
        paramMap.add("compere","aa");
        paramMap.add("meetingSummary",ccpcGroupZzsh.getMeetingSummary());
        paramMap.add("token",bhToken);
        HttpEntity< MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, headers);
            //执行HTTP请求
        String url="http://"+host+"/bhccpc/zzsh/newCcpcGroupZzsh";
        ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity,String.class );
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

json格式: 以 HttpEntity json字符串形式传过去

  HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 设置以json的方式提交
        headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        headers.add("token",loginUser.getBhToken());
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        //将请求头部和参数合成一个请求
       Map paramMap = new HashMap<>();
        paramMap.put("page",String.valueOf(pageParams.getCurrent()));
        paramMap.put("rows",String.valueOf(pageParams.getSize()));
        paramMap.put("userId",String.valueOf(memberId));
        JSONObject jsonObj = new JSONObject(paramMap);
        HttpEntity<String> requestEntity = new HttpEntity<>(jsonObj.toString(), headers);
        //执行HTTP请求,将返回的结构使用ResultVO类格式化
        String url="http://"+host+"/bhccpc/partyfee/detailMemberByPage";
        String response = restTemplate.postForObject(url,  requestEntity,String.class );
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

文件参数:

 //获取登录人
        CurrentUser loginUser = loginUserService.findLoginUser();
        String token = loginUser.getBhToken();
        Long userId = loginUser.getUserId();
        BizAssert.isTrue(loginUser != null, "请先登录!");

        String currentTimeStr = String.valueOf(System.currentTimeMillis());
        //组织生活会
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 以表单的方式提交

        headers.add("Content-Type",MediaType.MULTIPART_FORM_DATA_VALUE);
        headers.add("token", token);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        try {
            byte[] bytes  = file.getBytes();
            ByteArrayResource fileAsResource = new ByteArrayResource(bytes) {
                @Override
                public String getFilename() {
                    return file.getOriginalFilename();
                }

                @Override
                public long contentLength() {
                    return file.getSize();
                }
            };
            paramMap.add("file",fileAsResource);
        } catch (IOException e) {
            e.printStackTrace();
            throw  new BizException("上传失败");
        }

        paramMap.add("token",token);
        HttpEntity< MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, headers);
        //执行HTTP请求,将返回的结构使用ResultVO类格式化
        String url="http://"+host+"/bhccpc/file/savefile";
        ResponseEntity<String> result = restTemplate.exchange(url, method, requestEntity,String.class );

        return R.success(result);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

x-www-form-urlencoded

 public void  aaa() {
           url1="http://127.0.0.1:8080/getcityqoe" ;
                Map<String, String> headerMap = new HashMap<String, String>();
                headerMap.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            
            //参数
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("begintime", "2023-12-06 15:10"));
                params.add(new BasicNameValuePair("endtime", "2023-12-06 16:10"));
                params.add(new BasicNameValuePair("city","1"));
                params.add(new BasicNameValuePair("method", "getcityqoe"));

			RequestConfig reqConfig = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build();
					HttpResponse response=null;
					HttpClient httpclient = HttpClients.custom().setRetryHandler(myRetryHandler).build();
					try {
						HttpPost post = new HttpPost(url);
						// 将JSON进行UTF-8编码,以便传输中文
			
						if(!headMap.isEmpty()){
							for (Map.Entry<String, String> entry : headMap.entrySet()) {
								post.setHeader(entry.getKey(), entry.getValue());
							}
						}
						post.setEntity(new UrlEncodedFormEntity(params , "UTF-8"));
						post.setConfig(reqConfig);
						response=httpclient.execute(post);
						System.out.println(response);
						return EntityUtils.toString(response.getEntity(),"UTF-8");
					} catch (Exception e) {
						e.printStackTrace();
						return null;
					}finally{
						httpclient.getConnectionManager().shutdown();
					}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/302904
推荐阅读
相关标签
  

闽ICP备14008679号