欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring调用第三方接口

程序员文章站 2024-01-28 12:46:34
...

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<Object> entity = new HttpEntity<>(userData, headers);

ResponseEntity resp = HttpUtil.restTemplate.exchange(sendMessageUrl, HttpMethod.POST, entity, Object.class);
						
  • content-type:application/x-www-form-urlencoded
MultiValueMap<String, String> 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<Object> entity = new HttpEntity<>(postParameters, headers);

ResponseEntity responseEnt