如何将我的其余API映射到根上下文,从而从CXF/JAX-RS提供静态内容? [英] How can I serve static content from CXF / JAX-RS with my rest API mapped to root context?

查看:92
本文介绍了如何将我的其余API映射到根上下文,从而从CXF/JAX-RS提供静态内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用CXF来实现JAX-RS的rest API,其中REST端点直接在根上下文上.

I have a rest API using CXF to implement JAX-RS where the REST endpoints are directly on the root context.

例如,如果我的根上下文是localhost:8080/myservice

For example if my root context is localhost:8080/myservice

我的端点是:
本地主机:8080/myservice/resource1
本地主机:8080/myservice/resource2

And my endpoints are:
localhost:8080/myservice/resource1
localhost:8080/myservice/resource2

但是我想提供这样的静态内容:
本地主机:8080/myservice/docs/swagger.json

But I want to serve static content like this:
localhost:8080/myservice/docs/swagger.json

在我的web.xml中,我想做这样的事情:

In my web.xml I'd like to do something like this:

<servlet-mapping>
  <servlet-name>CXFServlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>/docs/*</url-pattern>
</servlet-mapping>

但是那不起作用,CXFServlet接收所有请求,而且我找不到一种方法来配置CXF/JAX-RS来提供我的静态内容,而又不包括新库和创建字节流等.我不这样做.不想做.我只想映射到默认的servlet.

But that doesn't work, the CXFServlet picks up all requests and I could not find a way to configure CXF / JAX-RS to serve my static content without including new libraries and creating byte streams, etc. which I don't want to do. I want to just map to the default servlet.

CXF文档不容易理解,我尝试执行以下操作未成功:

The CXF documentation is not easy to follow, and I tried unsuccessfully to do the following:

<servlet>
  <servlet-name>CXFServlet</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <init-param>
    <param-name>static-resources-list</param-name>
    <param-value>
      /docs/(\S)+\.html
      /docs/(\S)+\.json
    </param-value>
  </init-param>
</servlet>

有什么想法吗?

推荐答案

由于下面是我的web.xml中的servlet配置,用于通过映射到根目录的CXFServlet提供静态资源.

Below is my servlet config in my web.xml to serve static resources with a CXFServlet that is mapped to root.

<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <init-param>
        <param-name>redirects-list</param-name>
        <param-value>
          /docs/(\S)+\.html
          /docs/(\S)+\.json
    </param-value>
    </init-param>
    <init-param>
        <param-name>redirect-attributes</param-name>
        <param-value>
          javax.servlet.include.request_uri
    </param-value>
    </init-param>
    <init-param>
        <param-name>redirect-servlet-name</param-name>
        <param-value>default</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

希望这对其他人有帮助.

Hope this helps someone else.

这篇关于如何将我的其余API映射到根上下文,从而从CXF/JAX-RS提供静态内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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