【问题标题】:How to save and display a json/octet-stream response from a server in spring boot REST service?如何在 Spring Boot REST 服务中保存和显示来自服务器的 json/octet-stream 响应?
【发布时间】:2018-01-03 07:54:19
【问题描述】:

这是我正在使用的代码:

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

【问题讨论】:

  • 这段代码有什么异常吗?
  • 你应该用这个Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM}));替换

标签: json rest spring-boot


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-11-09
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2017-10-11
    • 2020-12-24
    • 2016-05-19
    • 2018-11-29
    相关资源
    最近更新 更多