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();

 

posted @ 2019-01-05 11:43  张永光  阅读(26880)  评论(0编辑  收藏  举报