简体   繁体   中英

How to save and display a json/octet-stream response from a server in spring boot REST service?

This is the code I'm using:

    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);             

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM