首页 > 解决方案 > Jax-RS /cxf 随机空响应

问题描述

我有一个资源 JAX-RS,它应该从 url 返回图片。它运作良好,但有时对于具有 http 200 代码的相同 url 的响应是空的。

我该如何解决这个问题,谢谢

httpd 日志:

100.xx.xx.xx - - [11/Jun/2020:16:27:46 +0200] [GET /ressources/1000/1 HTTP/1.1] 200 - [http://r-dev.fr/] [Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0]
100.xx.xx.xx - - [11/Jun/2020:16:27:46 +0200] [GET /ressources/1000/1 HTTP/1.1] 200 - [http://r-dev.fr/] [Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0]
                <groupId>javax.ws.rs</groupId>
                <artifactId>javax.ws.rs-api</artifactId>
                <version>2.0-m10</version>
            </dependency>   
              <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxrs</artifactId>
                <version>2.7.0</version>
            </dependency>
    public Response getPicture(final String url) {
        WebClient client = WebClient.create(url);
        try {

            Response r = client.get();
            if (r.getStatusInfo().getStatusCode() == Response.Status.OK.getStatusCode()) {
                String contentDispo = r.getStringHeaders().getFirst("Content-Disposition");
                if (!StringUtils.isEmpty(contentDispo)) {
                    ContentDisposition cd = new ContentDisposition(contentDispo);
                    String fileName = cd.getParameter("filename");
                    String mimeType = defineMimeType(fileName);
                    Object entity = r.getEntity();
                    return Response.ok(entity, mimeType).header("Connection", "Keep-Alive").header("Keep-Alive", "timeout=60, max=100").build();
                } else {
                    return Response.serverError().build();
                }
            } else {
                return r;
            }
        } catch (WebApplicationException | ClientException ex) {
            throw new WebApplicationException();
        }
    }

Api 使用方法从 url 获取图像:

 public Response getRessource(String idRessource) {    File fileToReturn = ....
    return Response.status(Response.Status.OK).entity(new FileInputStream(fileToReturn)).type("application/octet-stream")
    .header("Content-Disposition", "attachment; filename=" + fileToReturn.getName()).build();  
  }
  catch (FileNotFoundException e) {
    LOGGER.error("Erreur : le fichier d'id {} est introuvable", idRessource, e);
    return Response.status(Response.Status.NOT_FOUND).build();
  } 
}

标签: javajax-rscxf

解决方案


推荐阅读