Java index.html 404-在tomcat服务器中找不到
我有一个Tomcat服务器,正在尝试访问WEB-INF文件夹中的index.html文件,如下图所示 如图所示,当我打开Java index.html 404-在tomcat服务器中找不到,java,tomcat,Java,Tomcat,我有一个Tomcat服务器,正在尝试访问WEB-INF文件夹中的index.html文件,如下图所示 如图所示,当我打开http://localhost:9999/index.html它抛出一个404 这是我的web.xml文件: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jc
http://localhost:9999/index.html
它抛出一个404
这是我的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="CERTHMaTHiSiSOpenAPI" version="3.1">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>OpenAPI</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
api.ws;api.ws.oper
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>api.ws.oper.Authorization</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>OpenAPI</servlet-name>
<url-pattern>/api/*</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
您应该将html内容放在WebContent文件夹中,而不是web-inf中。因为它在WebContent中搜索index.html,而它不在那里,所以显示404 not found错误 看看这个问题有相似的情况和可能的答案
默认情况下,当您尝试使用
http://localhost:9999/index.html
如果要更改tomcat默认加载页面,请编辑server.xml
文件并更新以下内容
<Context path="" docBase="new_default_app" />
解决方案是将所有swagger ui文件移动到
WebContent
下的一个新文件夹(我称之为ui
),将web.xml
移动到web-INF
,并在web.xml
中添加一个简单的servlet映射:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/ui/*</url-pattern>
</servlet-mapping>
违约
/用户界面/*
问题是OpenAPI servlet中的
/api/*
url模式被api使用。我也有类似的问题。我对存储在WebContent中的html头版和Java代码生成的servlet感到困惑
Tomcat在WebContent中搜索.html
文件,如上下文参数所示。servlet映射的url参数告诉Tomcat在哪里查找servlet本身。对我来说,这两个地点需要不同是合乎逻辑的
我的servlet映射看起来像
/api/*
。我的应用程序类包含@Path('/sname')
。在保持WEB-INF/WEB.xml
尽可能简单的同时,这使我能够访问localhost:8080/
上的html页面和localhost:8080/api/sname上的servlethttp://localhost:9999/OpenAPI/index.html
工作?@StéphaneAmmar我试过了,如果您的上下文根目录是api
,则您的URL应该是http://localhost:9999/api/index.html
是的,我们试过了,但不起作用。它在WebContent下。这是一个不错的评论,但不是对这个问题的回答。OP直接使用URL(http://localhost:9999/index.html
),欢迎文件列表与此无关。没有足够的声誉发表评论@ErwinBolwidtMy server.xml文件包含了这一行。我删除了它并添加了你的,但当我试图启动服务器时,它抛出了错误。还应该注意的是,在项目属性>Web项目设置中,上下文根是api
您是否检查了您的服务器端口号?在server.xml中,有这样一行:
请在访问url时发布tomcat服务器错误的堆栈跟踪,很容易找到根本原因,没用。这是我删除此答案第一条评论中提到的行并在server.xml
中添加您的行时的错误日志:我的knownledge web.xml仅设置在web-INF下。我认为这是您的主要问题。正如我在问题中提到的,我尝试将其移动到web-INF,但没有成功,但这绝对是问题之一。
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/ui/*</url-pattern>
</servlet-mapping>