SpringのrestTempleteを使ってHttp要求を行う


最近はSpringフレームワークを勉強していますが、本文ではSpringを使ったrestTempleteにHttp要請をし、メモを残すことを紹介しています。

/*
* rest templete
*/
public class RestTemplateTest {
  //private static String url = "http://10.2.1.46:8080";
  private static String url = "http://127.0.0.1:8080/test";
  private static String autoDeploy = "/api/ddd/manages/yyy/1111";

  public static void main(String[] args) throws UnsupportedEncodingException {
    JSONObject token1 = LoginTest.login(url);
    JSONObject token = new JSONObject();
    token.put("token", token1.getString("token"));
    token.put("userName", "[email protected]");
    String name = "   ";
    token.put("name", URLEncoder.encode(name, "UTF-8"));

    String jsonBody = DmsAutoDeployTest.dmsAutoDeployData();
    autoDeploy(jsonBody,token.toJSONString());
  }

  /*
  *    post  
  */
  public static void autoDeploy(String json,String token){
    String autoDeployUrl = url + autoDeploy;
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    //      ContentType utf8     
    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
    headers.setContentType(type);
    headers.add("Accept", MediaType.APPLICATION_JSON.toString());
    headers.add("Authentication", token);//     session header

    HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);

    Map<String, Object> parameterMap = new HashMap<>();
    //entity = Header,uriVariables = Parameters;
    restTemplate.postForObject(autoDeployUrl, formEntity, String.class, parameterMap);
  }

  /*
  *   
  */
  public static void test4(){
      JSONObject response = null;
      Map<String,Object> param = new HashMap<>();
      param.put("ming", "chyen");

      String json = "haha";
      try {
        response = RestUtil.post("http://127.0.0.1:8080/cloud-master/api/release/manages/detail", JSONObject.class,null,null,json);
      } catch (Exception e) {
        e.printStackTrace();
      }
      System.out.println(response);
  }
  /**
   *   
   */
  public static void test(){
    RestTemplate restTemplate = new RestTemplate();

    Map<String, Object> parameterMap = new HashMap<>();
    restTemplate.getForObject("url", String.class,parameterMap);
    //factory.createRequest(uri, httpMethod)
  }
}

Util

/**
 *   Spring restTemplate  http  
 * @author Mingchenchen
 *
 */
public class RestUtil {
  private static RestTemplate restTemplate = new RestTemplate();

  /**
   * Get  
   * 
   * @param url:  
   * @param returnClassName:      , :String.class
   * @param parameters:parameter  
   * @return
   */
  public static <T> T get(String url, Class<T> returnClassName, Map<String, Object> parameters){
    if (parameters == null) {
      return restTemplate.getForObject(url, returnClassName);
    }
    return restTemplate.getForObject(url, returnClassName, parameters);
  }

  /**
   * post  ,     ,    ,Header,Parameter
   * 
   * @param url:  
   * @param returnClassName:      , :String.class
   * @param inputHeader
   * @param inputParameter
   * @param jsonBody
   * @return
   */
  public static <T> T post(String url,Class<T> returnClassName,Map<String,Object> inputHeader,Map<String,Object> inputParameter,String jsonBody){
    //  Header
    HttpHeaders httpHeaders = new HttpHeaders();
    //  Header
    if (inputHeader != null) {
      Set<String> keys = inputHeader.keySet(); 
      for (Iterator<String> i = keys.iterator(); i.hasNext();) { 
        String key = (String) i.next(); 
        httpHeaders.add(key, inputHeader.get(key).toString()); 
      } 
    }
    //          
    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
    httpHeaders.setContentType(type);
    httpHeaders.add("Accept", "application/json");
    List<MediaType> acceptableMediaTypes = new ArrayList<>();
    acceptableMediaTypes.add(MediaType.ALL);
    httpHeaders.setAccept(acceptableMediaTypes);

    HttpEntity<String> formEntity = new HttpEntity<String>(jsonBody, httpHeaders);
    if (inputParameter==null) {
      return restTemplate.postForObject(url, formEntity, returnClassName);
    }
    return restTemplate.postForObject(url, formEntity, returnClassName, inputParameter);
  }
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。