如何在Spring启动REST服务中保存并显示来自服务器的json / octet-stream响应?

时间:2018-01-03 07:54:20

标签: json rest spring-boot

这是我正在使用的代码:

    RestTemplate restTemplate = new RestTemplate();
    String fooResourceUrl = "server URI";

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    String auth = "username:password";
    byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")) );
    String authHeader = "Basic " + new String( encodedAuth );

    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("username", "password"));
    ResponseEntity<String> result = restTemplate.exchange(fooResourceUrl, HttpMethod.GET, entity, String.class);

    System.out.println(result);             

1 个答案:

答案 0 :(得分:0)

已解决::仅返回“结果”。