@Slf4j
public class HttpRequest {
// HttpConnectionConfig config = new HttpConnectionConfig();
public void syncPostRequest(String url, HashMap<String, Object> param){
// Crate Headers
HttpHeaders headers = new HttpHeaders();
// Content-Type : application/json
headers.setContentType(MediaType.APPLICATION_JSON);
// Accept : application/json
// headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
try{
// Build the Request
HttpEntity<String> entity = new HttpEntity<>(new JSONObject(param).toString(), headers);
// Send POST Request
String response = new RestTemplate().postForObject(url, entity, String.class);
JSONParser parser = new JSONParser();
Object obj = parser.parse(response);
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.get("contents"));
}catch (Exception e){
e.printStackTrace();
}
}
public JSONObject syncPostRequestReturnJSON(String url, HashMap<String, Object> param){
// Crate Headers
HttpHeaders headers = new HttpHeaders();
// Content-Type : application/json
headers.setContentType(MediaType.APPLICATION_JSON);
// Accept : application/json
// headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
try{
// Build the Request
HttpEntity<String> entity = new HttpEntity<>(new JSONObject(param).toString(), headers);
// Send POST Request
String response = new RestTemplate().postForObject(url, entity, String.class);
JSONParser parser = new JSONParser();
Object obj = parser.parse(response);
JSONObject jsonObject = (JSONObject) obj;
return jsonObject;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
@Async
public void asyncPostRequest(String url, HashMap<String, Object> param){
// Crate Headers
HttpHeaders headers = new HttpHeaders();
// Content-Type : application/json
headers.setContentType(MediaType.APPLICATION_JSON);
// Accept : application/json
// headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
try{
// Build the Request
HttpEntity<String> entity = new HttpEntity<>(new JSONObject(param).toString(), headers);
// Send POST Request
String response = new RestTemplate().postForObject(url, entity, String.class);
// System.out.println(response);
}catch (Exception e){
e.printStackTrace();
}
}
}