简体   繁体   English

将图像从Jersey REST服务返回到浏览器与使用servlet

[英]Return an image from Jersey REST service to browser vs. use servlet

I am curious to know what is the best way to return image (*.jpg or *.gif) to the client browser: 我很想知道将图像(* .jpg或* .gif)返回到客户端浏览器的最佳方法是什么:

  • The first idea came up in my mind is to use the good old servlet. 我想到的第一个想法是使用良好的旧servlet。
  • Or I can use JAX-RS (Jersy) rest service as well. 或者,我也可以使用JAX-RS(Jersy)休息服务。

I need to create some methods for different size of images like 我需要为不同大小的图像创建一些方法,例如

  • thumbnails size 缩略图大小
  • small size 小尺寸
  • normal size 正常尺寸
  • original size 原始尺寸
  • custom size 自定义大小

If I use servlet then I can create one servlet per image size and an extra one for custom size where I can send the desired W and H values as a URL parameters to the servlet. 如果使用servlet,则可以为每个图像大小创建一个servlet,并为自定义大小创建一个servlet,在其中可以将所需的W和H值作为URL参数发送到servlet。

If I use REST the I can create a class with methods and for the custom size I can use Path parameters. 如果我使用REST,则可以使用方法创建类,对于自定义大小,可以使用Path参数。

But what about the performance? 但是性能如何呢? Which solution is better and why? 哪种解决方案更好,为什么?

I think both solutions are equivalent in terms of performance. 我认为两种解决方案在性能上都是等效的。 However, IMHO, JAX-RS is different from Servlet because it is declarative whereas the latter is procedural. 但是,恕我直言,JAX-RS与Servlet不同,因为它是声明性的,而后者是过程性的。

I would go for one endpoint only with a @QueryParam instead of a @PathParam . 我只会使用@QueryParam而不是@QueryParam来实现一个@PathParam The size of the image is more an attribute of it than a subresource as I think you said in your question. 正如我认为您在问题中所说的那样,图像的大小更多地是其属性而不是子资源。

With this solution (that I've already implemented once) you've got only one method with annotations that make it really easy to read. 使用此解决方案(我已经实现过一次),您只有一种带有批注的方法,该批注使它真正易于阅读。 For example: 例如:

@Path("images")
public interface ImageResource {
    enum Size {
        thumbnail, small, normal, original, custom
    }

    @GET
    @Path("/{id}")
    @Produces("image/jpeg")
    byte[] getImage(@PathParam("id") long id, @QueryParam("size") @DefaultValue("thumbnail") Size size, @QueryParam("width") Integer width, @QueryParam("height") Integer height);
}

Then if size == Size.custom check that both width and height are not null. 然后,如果size == Size.custom请检查宽度和高度是否都不为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将PNG图像从Jersey REST服务方法返回到浏览器 - How to return a PNG image from Jersey REST service method to the browser Jersey REST服务将响应作为带有下载窗口的图像返回 - Jersey REST service to return response as an image with download window 为servlet Jersey REST服务错误分配异常 - Allocate exception for servlet Jersey REST Service Error 如何指示从Jersey中的REST服务保存到浏览器的文件名? - How to indicate the file name for saving to the browser from a REST service in Jersey? 500-servlet球衣剩余服务的servlet.init()抛出异常 - 500 - servlet.init() for servlet jersey rest service threw exception 从 Spring Rest Controller 调用 Servlet 并从 Servlet 返回图像 - Calling a Servelet from Spring Rest Controller and return image from Servlet 如何在jersey rest Web服务中获取图像 - how to get an image in jersey rest web service Servlet [Jersey REST服务]的Servlet.service()java.lang.NullPointerException - Servlet.service() for servlet [Jersey REST Service] java.lang.NullPointerException 从Android连接到Jersey REST Web服务 - Connecting to Jersey REST Web Service from Android 如何从文件夹中的 JAVA 中的 rest API 中将图像返回到浏览器? - How to return an Image to browser in rest API in JAVA from folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM