使用 Jersey 2.22.2 提供静态内容
Serve static content with Jersey 2.22.2
我有一个使用 Jersey 开发的 REST Api,在 Json 输入/输出时一切正常。我正在消耗 Ajax.
但是,由于浏览器的跨域限制,我想在我的 WAR[=13= 上打包静态网站 (JS / Images / HTMLs / 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访问?
您可以使用不同的 url 休息模式。例如,对于所有 rest 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目录下的static文件夹,然后你就可以访问它了:http://localhost:8080/static/index.html.
我有一个使用 Jersey 开发的 REST Api,在 Json 输入/输出时一切正常。我正在消耗 Ajax.
但是,由于浏览器的跨域限制,我想在我的 WAR[=13= 上打包静态网站 (JS / Images / HTMLs / 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访问?
您可以使用不同的 url 休息模式。例如,对于所有 rest 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目录下的static文件夹,然后你就可以访问它了:http://localhost:8080/static/index.html.