springBoot RestTemplate二http post传递json参数的多种方法
目录 post请求json数据(添加请求头)接收端发送端测试post请求json数据(添加请求头)并传递表单参数请求端测试restTemplate.exchange post请求json数据(添加请求头)并传递表单参数请求端post请求json数据(添加请求头) 直接传递jsonString数据RestTemplateBuilder post请求直接...
·
目录
restTemplate.exchange post请求json数据(添加请求头)并传递表单参数
post请求json数据(添加请求头) 直接传递jsonString数据
RestTemplateBuilder post请求直接传递jsonString数据
post请求json数据(添加请求头)
接收端
@PostMapping("/callBackFor")
public JSONObject callBackFor(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
/*
* String data 不可以接受json字符串,只能接收名为data的参数
* @RequestBody String data 可以以json字符串的形式接收json字符串
* @RequestBody JSONObject jsonObject 可以接收json字符串并转化为JSONObject对象
* */
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {//传递json数据的时候request是没有表单参数的
String parameterName = parameterNames.nextElement();
System.out.println(parameterName + "===" + request.getParameter(parameterName));
}
System.out.println(jsonObject.toJSONString());//
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()){
String header = headerNames.nextElement();
System.out.println(header+"=="+request.getHeader(header));
}
//无论是@RequestBody String data还是@RequestBody JSONObject jsonObject 最后都要转换为后者,因此建议@RequestBody JSONObject jsonObject接收json字符串
Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
System.out.println("=====我是回调函数=====");
for (Map.Entry<String, Object> entry : entries) {
System.out.println(entry.getKey() + "--" + entry.getValue());
}
System.out.println("=====================");
JSONObject result = new JSONObject();
result.put("code", 1);
result.put("msg", "调用成功!");
return result;
}
发送端
/**
* post请求json数据(添加请求头)
*/
public static void test1(){
RestTemplate restTemplate = new RestTemplate();
//创建请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String url = "http://localhost:8087/callBackFor";
User student = new User("sansan",10);
HttpEntity<User> entity = new HttpEntity<User>(student, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
System.out.println(user);
}
测试
post请求json数据(添加请求头)并传递表单参数
接收端不变
请求端
/**
* post请求json数据(添加请求头)并传递表单参数
*/
public static void test2(){
RestTemplate restTemplate = new RestTemplate();
//创建请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String url = "http://localhost:8087/callBackFor?id={id}";
Map<String, String> params = new HashMap<String, String>();
params.put("id", "123");
User student = new User("sansan",10);
HttpEntity<User> entity = new HttpEntity<User>(student, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class, params);
String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
System.out.println(user);
}
测试
restTemplate.exchange post请求json数据(添加请求头)并传递表单参数
请求端
/**
* restTemplate.exchange post请求json数据(添加请求头)并传递表单参数
* exchange可以发送HttpMethod.POST,DELE,GET,PUT请求;
*/
public static void test3(){
RestTemplate restTemplate = new RestTemplate();
//创建请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String url = "http://localhost:8087/callBackFor?id={id}";
//params是url中出现的路径变量
Map<String, String> params = new HashMap<String, String>();
params.put("id", "123");
User student = new User("sansan",10);
//entity包含请求的对象和消息头;
HttpEntity<User> entity = new HttpEntity<User>(student, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class, params);
String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
System.out.println(user);
}
post请求json数据(添加请求头) 直接传递jsonString数据
/**
* post请求json数据(添加请求头) 直接传递jsonString数据
*/
public static void test4(){
RestTemplate restTemplate = new RestTemplate();
//创建请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//也可以这样设置contentType
//MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//headers.setContentType(type);
//加不加Accept都可以
//headers.add("Accept", MediaType.APPLICATION_JSON.toString());
String url = "http://localhost:8087/callBackFor";
User student = new User("sansan",19);
String jsonString = JSONObject.toJSONString(student);
System.out.println(jsonString);//{"age":10,"name":"sansan"}
HttpEntity<String> entity = new HttpEntity<String>(jsonString, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
System.out.println(user);
}
RestTemplateBuilder post请求直接传递jsonString数据
/**
* RestTemplateBuilder post请求json数据(添加请求头) 直接传递jsonString数据
*/
public static void test5(){
//RestTemplate restTemplate = new RestTemplate();
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
RestTemplate restTemplate = restTemplateBuilder.build();
//创建请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//也可以这样设置contentType
//MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//headers.setContentType(type);
//加不加Accept都可以
//headers.add("Accept", MediaType.APPLICATION_JSON.toString());
String url = "http://localhost:8087/callBackFor";
User student = new User("sansan",19);
String jsonString = JSONObject.toJSONString(student);
System.out.println(jsonString);//{"age":10,"name":"sansan"}
HttpEntity<String> entity = new HttpEntity<String>(jsonString, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
System.out.println(user);
}
![Logo](https://devpress.csdnimg.cn/79de2bf0b7994defa4242ef90d5513fa.jpg)
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)