码迷,mamicode.com
首页 > 编程语言 > 详细

RestTemplate -springwebclient

时间:2018-01-11 16:04:48      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:etc   wrap   headers   uem   dia   temp   timestamp   请求   class   

1 使用jar版本 - spring-web-4.3.8.RELEASE.jar

  场景:backend,post请求远端,header中加入accessToken,用于权限控制 

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("accessToken","70379b6968bf85e95819f119c74559c");

        HttpEntity entity = new HttpEntity<>(headers);
        ResponseEntity<Object> result = restTemplate.postForEntity(url, entity,Object.class);

        int httpStatusCode = result.getStatusCodeValue();
        Object body = result.getBody();
        logger.debug("%s. 取得返回值, httpResponseStatusCode: %s, httpResponseBody: %s",logTitle, httpStatusCode, body);

        String bodyString = JSON.toJSONString(body);
        logger.debug("%s. 返回结果, bodyString: %s", logTitle, bodyString);


map参数:

        MultiValueMap<String, String> bodyMap = new LinkedMultiValueMap<>();
        bodyMap.add("username", request.username);
        bodyMap.add("timestamp", request.timestamp);
        bodyMap.add("sign", request.sign);
        bodyMap.add("data", JSON.toJSONString(request.data));

        ResponseEntity<Object> result = restTemplate.postForEntity(url, bodyMap, Object.class);
        int httpStatusCode = result.getStatusCodeValue();
        Object body = result.getBody();
        

 

2 总结

  a) RestTemplate 本身也是一个 wrapper 其底层默认是 SimpleClientHttpRequestFactory -> this implementation that uses standard JDK facilities

    b) 关于返回值是 ResponseEntity<T>  

      c) map为 LinkedMultiValueMap ,不能是hashmap(?)

      d)RestTemplate 有异步版本 AsyncRestTemplate

RestTemplate -springwebclient

标签:etc   wrap   headers   uem   dia   temp   timestamp   请求   class   

原文地址:https://www.cnblogs.com/xingzc/p/8267262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!