目录
post请求json数据(添加请求头)
接收端
发送端
测试
post请求json数据(添加请求头)并传递表单参数
请求端
测试
restTemplate.exchange post请求json数据(添加请求头)并传递表单参数
请求端
post请求json数据(添加请求头) 直接传递jsonString数据
RestTemplateBuilder post请求直接传递jsonString数据
@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 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 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> entries = jsonObject.entrySet();
System.out.println("=====我是回调函数=====");
for (Map.Entry 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 entity = new HttpEntity(student, headers);
ResponseEntity responseEntity = restTemplate.postForEntity(url, entity, String.class);
String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
System.out.println(user);
}
接收端不变
/**
* 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 params = new HashMap();
params.put("id", "123");
User student = new User("sansan",10);
HttpEntity entity = new HttpEntity(student, headers);
ResponseEntity responseEntity = restTemplate.postForEntity(url, entity, String.class, params);
String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
System.out.println(user);
}
/**
* 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 params = new HashMap();
params.put("id", "123");
User student = new User("sansan",10);
//entity包含请求的对象和消息头;
HttpEntity entity = new HttpEntity(student, headers);
ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class, params);
String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
System.out.println(user);
}
/**
* 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 entity = new HttpEntity(jsonString, headers);
ResponseEntity responseEntity = restTemplate.postForEntity(url, entity, String.class);
String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
System.out.println(user);
}
/**
* 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 entity = new HttpEntity(jsonString, headers);
ResponseEntity responseEntity = restTemplate.postForEntity(url, entity, String.class);
String user = responseEntity.getBody();//{"msg":"调用成功!","code":1}
System.out.println(user);
}