代码
@Test
void test() {
System.out.println(1);
User user = new User();
user.setUserName("tom");
user.setLxfs("1328976545");
user.setTzqy("广州");
user.setYxds("佛山");
ResponseEntity<Result> resultResponseEntity = restTemplate.postForEntity("http://localhost:8080/add", user, Result.class);
System.out.println(resultResponseEntity);
}
@Test
void test2() {
ResponseEntity<Result> forEntity = restTemplate.getForEntity("http://localhost:8080/hello", Result.class);
System.out.println(forEntity);
}
post json格式并添加header
String url = "http://localhost:8080/query";
FileBean fileBean = new FileBean();
fileBean.setId(678);
fileBean.setXid("2");
String body = JSON.toJSONString(fileBean);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json;charset=UTF-8");
headers.add("id", "6666");
HttpEntity<String> requst = new HttpEntity<>(body, headers);
RestTemplate restTemplateHttps = new RestTemplate();
ResponseEntity<JSONObject> responseBody = restTemplateHttps.postForEntity(url, requst, JSONObject.class);
JSONObject httpBody = responseBody.getBody();
System.out.println("接口返回参数:" + httpBody);
获取HEADER
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
String id = request.getHeader("id");
return id;