RestTemplate发送json请求

Posted 永光-技术改变梦想

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RestTemplate发送json请求相关的知识,希望对你有一定的参考价值。

  JSONObject json = new JSONObject();
                    json.put("train_date", train_date);
                    json.put("from_station", fromStationCode);
                    json.put("to_station", toStationCode);
                    json.put("logFlag", logFlag);

                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON.toString());
                    HttpEntity<String> formEntity = new HttpEntity<String>(json.toString(), headers);
                    String s= restTemplate.postForEntity(url,formEntity,String.class).getBody();

 

以上是关于RestTemplate发送json请求的主要内容,如果未能解决你的问题,请参考以下文章