如何使用JAX-RS提供静态内容? [英] How to serve static content with JAX-RS?

查看:115
本文介绍了如何使用JAX-RS提供静态内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用JAX-RS Restlet扩展实现的自托管JAX-RS REST服务。



现在我必须提供静态内容,我想知道如何使用JAX-RS做到这一点。注意,我在编译时不知道物理目录结构。所以,给定一个像



的网址

  http:// bla-bla:8182 / static / yaba / daba / doo.png 

文件 $(ROOT)/yaba/daba/doo.png ,其中 $(ROOT)是静态内容根目录。



是否可以使用纯JAX-RS进行操作?



谢谢。



EDIT



编译时已知:




  • 文件系统路径静态内容根文件夹

  • 用于引用静态内容根文件夹的HTTP URL



未知编译时:




  • 根文件夹的实际内容 - 文件数量,文件类型,目录结构。


解决方案

刚刚找到它。



根据 javax.ws.rs.Path 注释javadocs可以指定一个正则表达式来指示什么被认为是模板参数匹配。



因此,以下代码有效:

  @Path (static)
公共类StaticContentHandler {
...
@GET
@Path({path:。*})
public FileRepresentation Get( @PathParam(path)字符串路径){
...;
}
}

GET http:// localhost:8182 / static / yaba / daba / doo.png 使用路径获取方法c>等于yaba / daba / doo.png - 正是我所寻找的。

希望它可以帮助任何人。



BTW, FileRepresentation 属于Restlet,所以真正纯粹的JAX-RS实现会返回其他内容。


I have a self hosted JAX-RS REST service implemented with the JAX-RS Restlet extension.

Now I have to serve static content and I was wondering how to do it with JAX-RS. Note, that I do not know the physical directory structure at compile-time. So, given a URL like

http://bla-bla:8182/static/yaba/daba/doo.png

the file $(ROOT)/yaba/daba/doo.png has to be returned, where $(ROOT) is the static content root directory.

Is it possible to do it with pure JAX-RS?

Thanks.

EDIT

Known at compile-time:

  • File system path of the static content root folder
  • HTTP URL used to reference the static content root folder

Unknown at compile-time:

  • The actual content of the root folder - how many files, file types, directory structure.

解决方案

Just found it.

According to the javax.ws.rs.Path annotation javadocs one can specify a regex to indicate what is considered to be the template parameter match.

Hence, the following code works:

@Path("static")
public class StaticContentHandler {
  ...
  @GET
  @Path("{path:.*}")
  public FileRepresentation Get(@PathParam("path") String path) {
    ...;
  }
}

GET http://localhost:8182/static/yaba/daba/doo.png reaches the Get method with path equal to "yaba/daba/doo.png" - just what I was looking for.

Hope it helps anyone.

BTW, FileRepresentation belongs to Restlet, so a really pure JAX-RS implementation would return something else here.

这篇关于如何使用JAX-RS提供静态内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆