RestTemplate 模拟URL请求,处理多个请求参数,设置head中cookie

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/dainandainan1/article/details/80681321
注意点设置 headers.add("Accept", MediaType.APPLICATION_JSON.toString());没设置情况下,Spring后台无法匹配相应的json参数
多个参数采用MultiValueMap<String, Object> params
    @Test
public void searchActionTest(){
String paratemer_dp = "{\n" +
" \"source\": 2,\n" +
" \"shopId\": 0,\n" +
" \"shopGroupId\": 0,\n" +
" \"userId\": 0,\n" +
" \"nickName\": \"\",\n" +
" \"userMobile\": \"\",\n" +
" \"reviewIds\": [\n" +
" 29043375\n" +
" ],\n" +
" \"ip\": \"\",\n" +
" \"searchReviewType\": -1,\n" +
" \"reviewType\": -1,\n" +
" \"deleteReason\": 0,\n" +
" \"deleteUserId\": 0,\n" +
" \"startDate\": 0,\n" +
" \"endDate\": 0,\n" +
" \"stars\": [],\n" +
" \"shopTypes\": [],\n" +
" \"page\": 1,\n" +
" \"maxResult\": 10\n" +
"}";
String paratemer_mt = "{\n" +
" \"source\": 1,\n" +
" \"shopId\": 0,\n" +
" \"shopGroupId\": 0,\n" +
" \"userId\": 0,\n" +
" \"nickName\": \"\",\n" +
" \"reviewIds\": [\n" +
" 1080002131\n" +
" ],\n" +
" \"ip\": \"\",\n" +
" \"searchReviewType\": -1,\n" +
" \"reviewType\": -1,\n" +
" \"deleteReason\": 0,\n" +
" \"deleteUserId\": 0,\n" +
" \"startDate\": 0,\n" +
" \"endDate\": 0,\n" +
" \"stars\": [],\n" +
" \"shopTypes\": [],\n" +
" \"page\": 1,\n" +
" \"maxResult\": 10\n" +
"}";

JSONObject searchReqBeanJson = JSONObject.fromObject(paratemer_dp);
SearchReqBean searchReqBean = (SearchReqBean) JSONObject.toBean(searchReqBeanJson,SearchReqBean.class);

//todo 写一片博客介绍如何验证进入成功,爬虫再次试试
String url = "http://localhost:8080/api/review/adminOper/search";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
/*s*/
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
List<String> cookies = new ArrayList<>();
cookies.add("ssoid=" + "645160138b*24957a6f60c4505cd8995");
headers.put(HttpHeaders.COOKIE,cookies);
headers.put("access-token",Arrays.asList("645160138b*24957a6f60c4505cd8995"));
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
//设置前台参数
params.add("req", searchReqBean);
RestTemplate restTemplate = new RestTemplate();
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity(searchReqBean,headers);
ResponseEntity<BaseRspBean> baseRspBean = restTemplate.postForEntity(url, httpEntity, BaseRspBean.class);

logger.info("result:{}",baseRspBean);
}
展开阅读全文

没有更多推荐了,返回首页