[英]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)返回到客户端浏览器的最佳方法是什么:
I need to create some methods for different size of images like 我需要为不同大小的图像创建一些方法,例如
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.