小编Abh*_*rma的帖子

如何在java中使用resttemplate传递键值对

我要在post请求的主体中传递键值对.但是当我运行我的代码时,我得到错误为"无法写入请求:找不到合适的HttpMessageConverter请求类型[org.springframework.util.LinkedMultiValueMap]和内容类型[text/plain]"

我的代码如下:

MultiValueMap<String, String> bodyMap = new LinkedMultiValueMap<String, String>();
bodyMap.add(GiftangoRewardProviderConstants.GIFTANGO_SOLUTION_ID, giftango_solution_id);
bodyMap.add(GiftangoRewardProviderConstants.SECURITY_TOKEN, security_token);
bodyMap.add(GiftangoRewardProviderConstants.REQUEST_TYPE, request_type);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(bodyMap, headers);

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> model = restTemplate.exchange(giftango_us_url, HttpMethod.POST, request, String.class);
String response = model.getBody();
Run Code Online (Sandbox Code Playgroud)

java resttemplate

26
推荐指数
1
解决办法
3万
查看次数

标签 统计

java ×1

resttemplate ×1