【发布时间】:2020-12-11 18:09:42
【问题描述】:
我有一个第三方 API,它以 png 格式返回图像作为响应。我正在使用 Rest 模板调用该 API 并以 Byte[] 获取响应,但我遇到了这个异常
There was an unexpected error (type=Bad Request, status=400).JSON parse error: Can not deserialize instance of java.lang.Byte[] out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Byte[] out of START_OBJECT token at [Source: java.io.PushbackInputStream@7c9dac50; line: 1, column: 1]
我已经编写了以下代码来使用其余模板获取图像
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "*/*");
headers.set("Authorization","Bearer "+bearerToken);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<Byte[]> response = restTemplate.exchange(endPoint,HttpMethod.GET,entity,Byte[].class);
如何使用 java 中的 rest 模板从 API 中获取图像?
【问题讨论】:
标签: java spring-boot spring-mvc