【问题标题】:How to Re-direct to API index page when accessing API Base Path?访问 API 基本路径时如何重定向到 API 索引页面?
【发布时间】:2020-08-11 05:38:56
【问题描述】:

我正在开发一个带有 REST 服务的 Java 动态 Web 项目。

http://localhost:8080/fire_monitor 加载 index.html 。 但我希望http://localhost:8080/fire_monitor/api 也被重定向到index.html

我怎样才能从 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"
    version="3.1">
      <display-name>FireAlram Monitoring System - REST Service</display-name>

  <servlet>
    <servlet-name>fire_rest</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>fireservice</param-value>
    </init-param>

    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>fire_rest</servlet-name>
    <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
   </welcome-file-list>

</web-app>

【问题讨论】:

    标签: java rest web.xml


    【解决方案1】:

    我修好了。

    1. 将 index.html 重命名为 index.jsp(文件本身和 web.xml 欢迎文件)

    2. 将以下内容添加到 web.xml

    <servlet>
        <servlet-name>index.jsp</servlet-name>
        <jsp-file>/index.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
         <servlet-name>index.jsp</servlet-name>
         <url-pattern>/api</url-pattern>
    </servlet-mapping> 
    

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 2021-04-28
      • 2016-12-03
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 2019-10-25
      相关资源
      最近更新 更多