我正在使用Postman调用一个rest API,它使用以下请求给出了一个成功的响应(200 OK),
方法:POST
授权=>类型:承载令牌=>令牌:saflsjdflj
Body => form-data => Key:companyId,Value:123456
但是当我在spring Boot 中使用rest模板调用这个API时,它给出了400个错误请求。代码如下所示,
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + token);
HashMap<String, String> requestBody = new HashMap<>();
requestBody.put("companyId", "123456");
HttpEntity requestEntity = new HttpEntity<>(requestBody, headers)
ResponseEntity<CompanyResponse> response = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, CompanyResponse.class);
字符串
1条答案
按热度按时间ef1yzkbh1#
您必须配置restTemplate:add FormHttpMessageConverter
字符串
在发送请求时,必须将
MediaType.APPLICATION_FORM_URLENCODED
设置为contentType,并使用MultiValueMap
而不是HashMap
作为请求体:型