使用Jersey 2.22.2服务静态内容 [英] Serve static content with Jersey 2.22.2

查看:131
本文介绍了使用Jersey 2.22.2服务静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Jersey开发的REST Api,当涉及到Json in/out时,一切工作正常.而且我正在使用Ajax.

I have a REST Api that I developed using Jersey, everything works fine when it comes to Json in / out. and that I'm consuming by Ajax.

但是,由于浏览器的跨域限制,我想在WAR中打包静态网站(JS/图片/HTML/CSS)

However, due to Cross-Domain limitations on the browsers, I'd like to package the static website (JS / Images / HTMLs / CSS) on my WAR

这是我的web.xml的样子:

This is how my web.xml looks like :

<?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 :

现在,当我尝试访问http://localhost:8080/static/index.html时,它将作为REST调用处理.

Now when I try to access http://localhost:8080/static/index.html it's processed as a REST call.

如何制作静态目录包并通过API进行访问?

How can I make the static directory packages and accessed through the API?

推荐答案

您可以使用其他网址格式进行休息.例如,对于所有其他api网址格式,都以rest开头. http://localhost:8080/rest/ap/客户.

You could use different url pattern for rest. For eample, for all rest api url pattern start with 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 文件夹,然后可以访问它:

For Static contents, you could static folder under WebContent directory, then you could access it :http://localhost:8080/static/index.html.

这篇关于使用Jersey 2.22.2服务静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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