使用RestTemplate消费Hybris User API获取指定用户的信息

假设我用Hybris API调用的方式,返回Backoffice里维护的这个用户的全部信息:

使用下面这段代码:

public static void getUserInfo() {
        
        String responseStr = null;

        RestTemplate template = new RestTemplate();
        try {
            String token = "48a42ca3-b73a-4640-b690-1c393c0003ea";
            String url = "https://jerrywang.com:9002/rest/v2/electronics/users/[email protected]";
            
            ResponseEntity response = template.exchange(url, HttpMethod.GET, new HttpEntity(createHeaders(token)), String.class);
            responseStr = response.getBody();
            System.out.println("response: " + responseStr);
        }catch (Exception e)
        {
            System.out.println("the exception message is: "+e.getMessage());
        }
    }

    private static HttpHeaders createHeaders(String token){
         return new HttpHeaders() {{
                 set( "Authorization", "Bearer " + token );
              }};
    }

测试结果:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":


你可能感兴趣的:(使用RestTemplate消费Hybris User API获取指定用户的信息)