SHARE
TWEET

Untitled

a guest Oct 21st, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.nostratech.bcaf.collector.util;
  2.  
  3. import com.google.gson.Gson;
  4. import com.nostratech.bcaf.collector.vo.ParamVO;
  5. import com.nostratech.bcaf.collector.vo.ResultVO;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.http.HttpEntity;
  9. import org.springframework.http.HttpHeaders;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.stereotype.Component;
  12. import org.springframework.web.client.RestTemplate;
  13.  
  14. import java.util.List;
  15.  
  16. /**
  17.  * Created by derryaditiya on 2/9/17.
  18.  */
  19. @Component
  20. public class WebServiceUtil {
  21.  
  22.     public static final Logger logger = LoggerFactory.getLogger(WebServiceUtil.class);
  23.  
  24.     public ResultVO requestPost(String url, Object payload) {
  25.         String jsonRequest = new Gson().toJson(payload);
  26.  
  27.         HttpHeaders headers = new HttpHeaders();
  28.         headers.setContentType(MediaType.APPLICATION_JSON);
  29.         HttpEntity<String> payloadHttpEntity = new HttpEntity<String>(jsonRequest, headers);
  30.  
  31.         ResultVO resultVO = new ResultVO();
  32.         try {
  33.             RestTemplate restTemplate = new RestTemplate();
  34.             resultVO = restTemplate.postForObject(url, payloadHttpEntity, ResultVO.class);
  35.         } catch (Exception e) {
  36.             logger.error(StatusCode.ERROR.name(), e);
  37.             resultVO.setMessage(StatusCode.ERROR.name());
  38.             resultVO.setResult(e);
  39.             return resultVO;
  40.         }
  41.  
  42.         return resultVO;
  43.     }
  44.  
  45.     public ResultVO requestGet(String url, List<ParamVO> paramVOList) {
  46.         String urlParam = "";
  47.         String urlComplete = "";
  48.         if (paramVOList != null) {
  49.             for (ParamVO param : paramVOList) {
  50.                 urlParam = urlParam + param.getKey() + "=" + param.getValue() + "&";
  51.             }
  52.  
  53.             urlComplete = url + "?" + urlParam;
  54.         } else {
  55.             urlComplete = url;
  56.         }
  57.         ResultVO resultVO = new ResultVO();
  58.  
  59.         try {
  60.             RestTemplate restTemplate = new RestTemplate();
  61.             resultVO = restTemplate.getForObject(urlComplete, ResultVO.class);
  62.         } catch (Exception e) {
  63.             logger.error(StatusCode.ERROR.name(), e);
  64.             resultVO.setMessage(StatusCode.ERROR.name());
  65.             resultVO.setResult(e);
  66.             return resultVO;
  67.         }
  68.  
  69.         return resultVO;
  70.     }
  71.  
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top