zoukankan      html  css  js  c++  java
  • springboot使用restTemplate post提交值 restTemplate post值

      post提交有 FormData和Payload  两种形式:
    第一种是formdata形式,在header参数里可以直接看到
     payload则封装成json格式post过去,获取以后需要再解析成实体。
     
    restTemplate  post json格式
    使用阿里巴巴的json包  com.alibaba.fastjson
    代码demo如下:
    url='http://posturl';JSONObject postData = new JSONObject();      
    postData.put("shopid", 1);
    JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody();
     
    如果要使用post  formdata形式则需要
    使用RestTemplate发送multipart/form-data格式的数据
    String url = 'http://posturl';
    MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
    map.add("shopid","1");
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
    return restTemplate.postForEntity(url, request,String.class);
     
    对header进行请求头设置,如果不设置也可以直接post那么就是如下的
     
    String url = 'http://posturl';       
     MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();        
     map.add("shopid","1");
    return restTemplate.postForEntity(url, map,String.class);
     
  • 相关阅读:
    linux & centos命令
    javascript总结
    SocanCode7之模板编写
    SocanCode连接Oracle的方法
    ashx的使用
    SocanCode代码生成器版本更新记录 [SocanCode7全新发布]
    IIS7.0中使用MVC3,静态页正常,其它404
    不用再纠结反射影响效率了
    ASP.NET MVC 框架处理请求生命周期
    create xmlhttprequest
  • 原文地址:https://www.cnblogs.com/xinweiyun/p/9348273.html
Copyright © 2011-2022 走看看