在spring restcontroller响应中返回ByteArrayResource

时间:2018-04-19 08:53:57

标签: java spring-mvc swagger spring-restcontroller spring-rest

我正在尝试使用我的restController下载文件,但它总是返回此错误:

 java.io.FileNotFoundException: Byte array resource [resource loaded from byte array] cannot be resolved to URL
at org.springframework.core.io.AbstractResource.getURL(AbstractResource.java:90) ~[spring-core-4.2.2.RELEASE.jar:4.2.2.RELEASE]
然后它会下载一个类似这样的文件:

  {"byteArray":"JVBERi0xLjQKJeL.... 

这是我的restController:

@Api("products")
@RestController
@RequestMapping("/v1/products")
public class DocumentApi extends storeApi {
    @ApiOperation("GET download document")
    @RequestMapping(value = "/temp", method = RequestMethod.GET)
    @ResponseStatus(code = HttpStatus.OK)
    public ResponseEntity<ByteArrayResource> downloadDocument(
        @RequestParam(value = "id", required = true) Long idInscription) throws IOException {
          String signedFilePAth = "C:/APPLIS/signedTemp/5982312957957647037_signed.pdf"
          return ResponseEntity
                 .ok()
                 .contentLength(contentLength)
                 .contentType(
                     MediaType.parseMediaType("application/pdf"))
                 .body(new 
          ByteArrayResource(Files.readAllBytes(Paths.get(signedFilePAth))));
    }
}

这是我的弹簧配置:

protected MappingJackson2HttpMessageConverter jacksonMessageConverter() {
    MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    // Registering Hibernate4Module to support lazy objects
    mapper.registerModule(new Hibernate4Module());
    messageConverter.setObjectMapper(mapper);
    return messageConverter;
}

public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
    ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
    arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes());
    return arrayHttpMessageConverter;
}

private List<MediaType> getSupportedMediaTypes() {
    List<MediaType> list = new ArrayList<MediaType>();
    list.add(MediaType.APPLICATION_OCTET_STREAM);
    list.add(MediaType.parseMediaType("application/pdf"));
    return list;
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(jacksonMessageConverter());
    converters.add(byteArrayHttpMessageConverter());
    super.configureMessageConverters(converters);
}

似乎是什么问题?我怎么能解决这个问题?

nb:我不知道这是否相关,但我的swagger-ui.html不起作用(显示空白页面),而v2 / api-docs /工作正常

2 个答案:

答案 0 :(得分:0)

尝试返回字节数组:

@RequestMapping(value = "/temp", method = RequestMethod.GET)
public @ResponseBody byte[] downloadDocument(
        @RequestParam(value = "id", required = true) Long idInscription) throws IOException {
    FileInputStream signedFileInputStream = new FileInputStream(signedFilePAth);
    byte[] doc = IOUtils.toByteArray(fis);
    return doc;
}

IOUtils来自org.apache.commons.io.IOUtils

答案 1 :(得分:0)

您的代码似乎缺少响应内容类型定义。

以下代码段返回一个由Web浏览器显示的图像内容。这是一个Jersy代码,但你可以将它采用到Spring:

@GET
@Path("/{image-uuid}")
@Produces("images/jpg")
public Response getImage(@PathParam("uuid") final String uuid) throws IOException {
    byte[] content = imageDao.getImage(uuid);
    if (Objects.isNull(content )) {
        throw new ImageNotFoundError(uuid);
    }

    ByteArrayInputStream stream = new ByteArrayInputStream(content);
    return Response.ok(stream).build();
}