zoukankan      html  css  js  c++  java
  • RestTemplate 请求url

    1.get 请求

    RestTemplate restTemplate = new RestTemplate();

    String url = "";
    JSONObject result = restTemplate.getForObject(url, JSONObject.class);

    2.post请求

    public static JSONObject postObject(String url,Map<String, Object> params){
      RestTemplate restTemplate = new RestTemplate();
      HttpHeaders headers = new HttpHeaders();
      MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
      headers.setContentType(type);
      headers.add("Accept", MediaType.APPLICATION_JSON.toString());
      headers.add("Authorization", "Basic "+String.valueOf(Base64.encode((appKey+":"+masterSecret).getBytes())));//某些特殊的请求需要验证参数

      JSONObject jsonObj = null;
      HttpEntity<String> formEntity = null;
      if(!EmptyUtil.isEmpty(params)){
        jsonObj = JSONObject.fromObject(params);
         formEntity = new HttpEntity<String>(jsonObj.toString(), headers);
      }
      JSONObject result = restTemplate.postForObject(url, formEntity, JSONObject.class);

      return result;
    }

  • 相关阅读:
    Linux之wget命令
    Markdown语法
    Windows实时预览markdown
    Python基础教程,Python入门教程(非常详细)
    【转载】UNICODE与ASCII的区别
    Python之虚拟环境
    Linux文件系统管理
    Linux权限管理
    linux用户和用户组管理
    linux 软件安装
  • 原文地址:https://www.cnblogs.com/difme/p/5464729.html
Copyright © 2011-2022 走看看