Java测试-RestTemplate-@requestBody

xiaoxiao2021-02-28  78

在java的test代码中,如果遇到了要传入@requestBody声明的参数时,可以使用以下代码

String infra = "{\"upperId\":110000,\"areaName\":\"测试BaseArea\",\"remark\":\"备注\"}"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.valueOf("application/json;UTF-8")); HttpEntity<String> strEntity = new HttpEntity<String>(infra,headers); RestTemplate restTemplate = new RestTemplate(); JSONObject jo2 = restTemplate.postForObject(url,strEntity,JSONObject.class); System.out.println(jo2);

infra为json字符串

strEntity为传递用的参数

提取自:http://blog.csdn.net/qwe6112071/article/details/51042634

转载请注明原文地址: https://www.6miu.com/read-81609.html

最新回复(0)