A智丨勇灬 2019-09-02 17:48 采纳率: 100%
浏览 4776
已采纳

postman中增加Authorization Bearer apiToken的方法通过代码怎么写呢?

图片说明

如上图,通过postman是可以调通的,通过springboot接口怎么增加token呢?

js和java都可以,感谢!
我希望通过restTemplate调用,代码片段

 Map resMap = restTemplate.postForObject(boostpayConfiguration.getUrl(), returnMap, Map.class);
  • 写回答

2条回答 默认 最新

  • A智丨勇灬 2019-09-04 10:08
    关注
    MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(boostpayConfiguration.getUrl());
            String jsonString = JSON.toJSONString(returnMap);
            StringEntity entity = new StringEntity(jsonString, "UTF-8");
            httpPost.setEntity(entity);
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
            httpPost.setHeader("Authorization", "Bearer " + boostPayVO.getApiToken());
            CloseableHttpResponse response = null;
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            log.info("=====响应状态为:{}", response.getStatusLine());
            if (responseEntity != null) {
                log.info("=====响应内容长度为:{}", responseEntity.getContentLength());
                String responseJson = EntityUtils.toString(responseEntity);
                log.info("=====响应内容为:{}", responseJson);
                return JSON.parseObject(responseJson, Map.class);
            }
    

    自己动手风衣足食

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?