欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解使用Spring的restTemplete进行Http请求

程序员文章站 2023-12-21 15:32:04
最近学习spring框架,本文介绍了使用spring的resttemplete进行http请求,留个笔记 /* * rest templete */ pub...

最近学习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", "chenchen.ming@56qq.com");
    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);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: