1. 程式人生 > >RestTemplate傳送json請求

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