接口返回图片使用什么类型_JavaWeb

Ashe / 2023-08-17 / 原文

    public ResponseEntity<byte[]> icon() {
        
        // 第三方接口返回的图片验证码
        ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
    
        …… // 由后端转发此请求,是因为此处需要做一些特殊处理    
    
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG); // 设置响应头为image/jpeg
        return new ResponseEntity<>(response.getBody(), headers, HttpStatus.OK);

    }

接口返回类型使用ResponseEntity<byte[]>即可