简体   繁体   English

在Spring Boot应用程序中通过Jersey资源方法提供静态内容

[英]Serving static content from Jersey resource method in a Spring Boot app

I'm using a combination of Spring Boot and JAX-RS for my REST API. 我为我的REST API使用了Spring Boot和JAX-RS的组合。 In my Spring Java Application I have an image located in my resources directory. 在我的Spring Java Application中,我的资源目录中有一个映像。 I want to serve this picture with an URL, it should be something like this: 我想用URL为这张图片提供服务,应该是这样的:

localhost:8080/api/get/img/1/imageFileName.png

Like this I should be able to use this URL in my Angular Frontend Application in an img tag: 这样,我应该可以在Angular Frontend Application的img标签中使用以下URL:

<img src="localhost:8080/api/get/img/1/imageFileName.png"/>

So the issue is, that I really want to have this stuff in my FileSystem. 所以问题是,我真的很想在我的FileSystem中拥有这些东西。 I definately do not want to return a byte array, but that's the only thing I could find so far. 我绝对不想返回字节数组,但这是到目前为止我唯一能找到的东西。 This is the only code I could come up with so far: 到目前为止,这是我唯一能想到的代码:

@GET
@Path("get/img/1")
public String getFile() {
    File file = Paths.get(".", "resources", "Mockup9EventsPageAsMember.png").normalize().toFile();
    return file.toString();
}

Obviously this will only return the path to the respective directory and not an URL link which I could use in my img tag. 显然,这只会返回到相应目录的路径,而不是我可以在img标签中使用的URL链接。

Any suggestions how I can tell my SpringBoot JAX-RS Application to create an URL for me? 有什么建议可以告诉SpringBoot JAX-RS Application为我创建URL吗?

I could give you a solution to what you are trying to do, but it is definitely not recommended. 我可以为您尝试做的事情提供解决方案,但是绝对不建议这样做。 The main reason would be the lack of caching. 主要原因是缺少缓存。 You would need to implement that yourself, which is already provided by the server if you let it serve all the static content. 您需要自己实现,如果让服务器提供所有静态内容,则服务器已经提供了该功能。

In a Spring Boot + Jersey app, to be able to serve static content, you need to have the spring-boot-starter-web dependency. 在Spring Boot + Jersey应用程序中,要能够提供静态内容,您需要具有spring-boot-starter-web依赖项。 Then after that you can put all your static resources into src/main/resources/static 1 . 然后,您可以将所有静态资源放入src/main/resources/static 1中 If you put a file /images/foobar.png in that directory, then you can access it via localost:8080/images/foobar.png . 如果将文件/images/foobar.png放在该目录中,则可以通过localost:8080/images/foobar.png

If you really really want to serve the static content from the resource method (which again I strongly recommend against), you don't have to return a byte[] . 如果你真的 真的想成为从资源的方法(这我再次强烈反对)的静态内容,您不必返回一个byte[] You could just return a File or an InputStream . 可以只返回FileInputStream Or you can use StreamingOutput (just search for some examples - you shouldn't have any problems finding any). 或者,您可以使用StreamingOutput (只需搜索一些示例-查找任何示例都应该没有问题)。 But again, with this method, you need to take care of sending all the correct caching headers, or else the browser will not cache the files and they will served each time they are requested, which just uses unnecessary resources. 但是,再次使用此方法,您需要注意发送所有正确的缓存头,否则浏览器将不会缓存文件,而是在每次请求文件时就将它们提供服务,而这只会使用不必要的资源。


See also 也可以看看

  • If you have problems accessing the static content, it might be related to this issue 如果您在访问静态内容时遇到问题,则可能与此问题有关

1. There are also more locations. 1.还有更多位置。 See the docs 查看文件

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM