spring resetTemplate 发送Json post 请求

原创
04/20 19:42
阅读数 113

1.使用resetTemplate 发送post请求(Content-Type=application/json)

public static void test3() {

		//headers
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.add("Authorization", "aaa");
		requestHeaders.add("Content-Type", "application/json");
		//body
//		MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
		Map<String,Object> requestBody = Maps.newHashMap();
		requestBody.put("name", "111);
		//HttpEntity
		HttpEntity<String> requestEntity = new HttpEntity<>(JSON.toJSONString(requestBody), requestHeaders);
		System.out.println("requestEntity:" + requestEntity);
		RestTemplate restTemplate = new RestTemplate();
		//post
		ResponseEntity<String> responseEntity = restTemplate.postForEntity("url", requestEntity, String.class);
		System.out.println(responseEntity.getBody());

	}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部