简体   繁体   English

使用Jersey 2.22.2服务静态内容

[英]Serve static content with Jersey 2.22.2

I have a REST Api that I developed using Jersey, everything works fine when it comes to Json in / out. 我有一个使用Jersey开发的REST Api,当涉及到Json in / out时,一切正常。 and that I'm consuming by Ajax. 而且我正在使用Ajax。

However, due to Cross-Domain limitations on the browsers, I'd like to package the static website (JS / Images / HTMLs / CSS) on my WAR 但是,由于浏览器的跨域限制,我想在WAR中打包静态网站(JS /图片/ HTML / CSS)

This is how my web.xml looks like : 这是我的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>

and my static content on the project structure : 和我在项目结构上的静态内容:

在此处输入图片说明

Now when I try to access http://localhost:8080/static/index.html it's processed as a REST call. 现在,当我尝试访问http://localhost:8080/static/index.html它将作为REST调用进行处理。

How can I make the static directory packages and accessed through the API? 如何制作静态目录包并通过API访问?

You could use different url pattern for rest. 您可以使用其他网址格式进行休息。 For eample, for all rest api url pattern start with rest. 对于示例,对于所有其余api url模式,以rest开头。 http://localhost:8080/rest/ap/customer . http:// localhost:8080 / rest / ap / customer

web.xml: 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>

For Static contents, you could static folder under WebContent directory, then you could access it : http://localhost:8080/static/index.html . 对于静态内容,您可以在WebContent目录下的静态文件夹中,然后可以访问它: http:// localhost:8080 / static / index.html

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

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