spring调用第三方接口

post请求

  • content-type:json
userData.put("accountid", "-1");
//设置请求头
HttpHeaders headers = new HttpHeaders();      
headers.add("Accept", "application/json");
headers.add("Content-Type", contentType);
//userData 是请求体body
HttpEntity entity = new HttpEntity<>(userData, headers);

ResponseEntity resp = HttpUtil.restTemplate.exchange(请求url, HttpMethod.POST, entity, Object.class);
						
 
  
  • content-type:application/x-www-form-urlencoded
MultiValueMap postParameters = new LinkedMultiValueMap<>();
postParameters.put("param1", Collections.singletonList(param1));

HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
HttpEntity entity = new HttpEntity<>(postParameters, headers);

ResponseEntity responseEntity = HttpUtil.res 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(spring)