[英]Serve static content with Jersey 2.22.2
我有一个使用Jersey开发的REST Api,当涉及到Json in / out时,一切正常。 而且我正在使用Ajax。
但是,由于浏览器的跨域限制,我想在WAR中打包静态网站(JS /图片/ HTML / CSS)
这是我的web.xml的样子:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="RESTApi"
version="3.1">
<display-name>RESTApi</display-name>
<servlet>
<servlet-name>Test -> Jersey RESTful API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.bc.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test -> Jersey RESTful API</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
和我在项目结构上的静态内容:
现在,当我尝试访问http://localhost:8080/static/index.html
它将作为REST调用进行处理。
如何制作静态目录包并通过API访问?
您可以使用其他网址格式进行休息。 对于示例,对于所有其余api url模式,以rest开头。 http:// localhost:8080 / rest / ap / customer 。
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="RESTApi"
version="3.1">
<display-name>RESTApi</display-name>
<servlet>
<servlet-name>Test -> Jersey RESTful API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.bc.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test -> Jersey RESTful API</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
对于静态内容,您可以在WebContent目录下的静态文件夹中,然后可以访问它: http:// localhost:8080 / static / index.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.