赞
踩
import com.dykj.common.utils.R;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
RestTemplate restTemplate=new RestTemplate();
String url="http://localhost:8080/demo/test";//接口url
//1.map中定义的是请求接口需要的参数 这个url只想要我这边定义的参数
String[] str=new String[]{"202306150001"};
Map<String,Object> list=new HashMap<>();
list.put("type","eq");
list.put("column","incomeId");
list.put("value",str);
List<Map<String,Object>> qmList=new ArrayList<>();
qmList.add(list);
Map<String,Object> map=new HashMap<>();
map.put("propertyId","0000000027097");
map.put("propertyType","0008");
map.put("incomeId","202306150001");
map.put("current",1);
map.put("size",9999);
map.put("queryMapList",qmList);
//2.对浏览器请求信息进行的跨域设置以及token参数
HttpHeaders headers = new HttpHeaders();
String token = "eyJhbGciOiJIUzUxMiIsInppcCI6IkdaSVAifQ." +
"H4sIAAAAAAAAAKtWKi5NUrJScon09tINDXYNUtJRSq0oULIyNLMws7SwNDU21VEqLU4t8kwBqjKAAEMliJhfYm4qUPT5uoXPJ7Q9nThDqRYAYjUhhE8AAAA." +
"1iMmzv-iJ1h5m4j7VQmaGYhqT5msdGPvNN6zn4m1qhV9qbkFWcItlaUekyDv3jzksgVs53HT9oVH70Trl409zA";
headers.add("Authorization",token);
headers.add("Access-Control-Allow-Origin","*");
headers.add("Access-Control-Allow-Methods","GET, HEAD, POST, OPTIONS");
headers.add("Access-Control-Allow-Headers","Content-Type");
//HttpEntity<Map<String,Object>> 中 Map<String,Object>是此url需要请求的对应格式请求参数,这里只想传map集合,你那边要根据你的情况选择
HttpEntity<Map<String,Object>> request = new HttpEntity<>(map,headers);
//利用restTemplate发送数据 post/get/put/delete 这里是post请求
ResponseEntity<R> forEntity = restTemplate.postForEntity(url,request,R.class);
System.out.println(forEntity.getBody());//如果有数据 getBody会显示
//<200,[Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", Content-Length:"0", Date:"Fri, 16 Jun 2023 07:49:48 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>
//上面的错误是没有配置 HttpHeaders信息或者没传new HttpEntity<>(map,headers) 中的map
}
}
需要发送的参数
接口返回结果
控制台打印
接口url
大功告成,帮到您的话,求打赏
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。