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 NFS服务器的安装与配置
    Genymotion加速下载虚拟镜像速度慢失败Connection timeout
    人脸识别的损失函数
    caffe服务器搭建血泪记录
    shell
    ZigZag Conversion
    SSH阅读笔记
    numpy数组扩展函数repeat和tile用法
    高效网络小结
    Bag of Tricks for Image Classification with Convolutional Neural Networks论文笔记
  • 原文地址:https://www.cnblogs.com/xinweiyun/p/9348273.html
Copyright © 2011-2022 走看看