给请求加上头信息
Request request = new Request(); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.add("accept", "application/json"); requestHeaders.add("content-type", "application/json"); requestHeaders.add("content-length", "256"); requestHeaders.add("authorization", authorization); requestHeaders.setContentType(MediaType.APPLICATION_JSON) HttpEntity<?> httpEntity = new HttpEntity<Object>(request, requestHeaders);
ResponseEntity<Response> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Response.class);
return response.getBody();
有些请求是键值对的形式,用request的方法或封装HttpEntity也传不进去参数,可以尝试用下面的方法:
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.add("shortcut", shortcut); map.add("_api_key", apikey);DefaultResponse response = restTemplate.postForObject(url, map, DefaultResponse.class);
时间: 2024-10-05 08:49:10