Java测试-RestTemplate-@requestBody

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

String infra = "{\"upperId\":110000,\"areaName\":\"测试BaseArea\",\"remark\":\"备注\"}";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
HttpEntity strEntity = new HttpEntity(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



你可能感兴趣的:(java)