java - index.html 404 - 在 tomcat 服务器中找不到

标签 java tomcat

我有一个 Tomcat 服务器,我正在尝试访问位于 WEB-INF 文件夹中的 index.html 文件,如下图所示

Project directory structure

如图所示,当我打开 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>

它位于 WEB-INF/lib 下是否有问题?我尝试将其移动到 WEB-INF,但没有成功。

编辑:这是我的 Properties > Web Project Settings 选项卡: Web Project Properties

应该注意的是,这个项目还有一个 API,在 Java Resources > src > ws 下,它有一个 Conductor.java 文件,内容如下:

package ws;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;

import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;
import io.swagger.models.Swagger;
import io.swagger.util.Json;
import ws.util.Config;


@ApplicationPath("/")
public class Conductor extends Application {

@Context ServletContext context;

    public static Properties properties = new Properties();

    public Conductor() {
        super();

        BeanConfig beanConfig = new BeanConfig();
        BeanConfig beanConfig2 = new BeanConfig();

        beanConfig.setVersion("3.1.1");
        beanConfig.setSchemes(new String[]{"https","http"});
//        beanConfig.setHost("localhost:8080");
        beanConfig.setBasePath("/api");
        beanConfig.setResourcePackage("ws.lg");
        beanConfig.setTitle("LG lib Open API");
        beanConfig.setScan(true);

        Swagger swaglg = new Swagger();
        swaglg = beanConfig.getSwagger();
        swaglg.setBasePath("/api/lg");

        beanConfig2.setVersion("3.1.1");
        beanConfig2.setSchemes(new String[]{"https","http"});
//        beanConfig.setHost("localhost:8080");
        beanConfig2.setBasePath("/api");
        beanConfig2.setResourcePackage("ws.sla");
        beanConfig2.setTitle("SLA lib Open API");
        beanConfig2.setScan(true);

        Swagger swaglg2 = new Swagger();
        swaglg2 = beanConfig2.getSwagger();
        swaglg2.setBasePath("/api/sla");

        createSwaggerJsonFile(beanConfig, "swagger-lg.json");
        createSwaggerJsonFile(beanConfig2, "swagger-sla.json");
    }

//  public static final int REST_PORT = 8080;
    @Override
    public Set<Class<?>> getClasses(){
        readProperties();

        Set<Class<?>> resources = new HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    private void addRestResourceClasses(Set<Class<?>> resources){
        resources.add(ws.sla.SLAlibOpenAPI.class);
        resources.add(ws.lg.LGlibOpenAPI.class);
        resources.add(ws.auth.Authorization.class);

        resources.add(ApiListingResource.class);
        resources.add(SwaggerSerializers.class);


        //to turn off
//      resources.add(ws.helpers.MongoModifiersAPI.class);
    }


    private Properties readProperties() {
        String fullPath = context.getRealPath(Config.PROPERTIES_FILE);
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(fullPath));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        if (inputStream != null) {
            try {
                properties.load(inputStream);
            } catch (IOException e) {
                // TODO Add your custom fail-over code here
                e.printStackTrace();
            }
        }else{
            System.err.println("Cannot read config file or no config file exists.");
        }
        return properties;
    }

//  /*
    private static void createSwaggerJsonFile(BeanConfig beanConfig, String filename) {
        try (FileWriter fileWriter = new FileWriter(filename)) {
            File f = new File(filename);
            fileWriter.write(Json.pretty(beanConfig.getSwagger()));
            fileWriter.flush();
            System.out.println("File " + filename + " successfully created in " + f.getAbsolutePath());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
//    */

}

最佳答案

您应该将 html 内容放在 WebContent 文件夹中,而不是放在 web-inf 中。因为它在 WebContent 中搜索 index.html 而它不存在,所以它显示 404 not found 错误。

看看这个问题有类似的情况和可能的答案 StackOverFlow Question

关于java - index.html 404 - 在 tomcat 服务器中找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48073477/

相关文章:

java - getPrimaryClip() 返回 ClipData { text/plain {NULL} }

java - 如何在intellij idea编辑器中删除顶行面包屑?

java - 无法使用 Morphia 连接到 MongoDb

java - 使用 spring 下载上传的文件。 Tomcat 服务器随机卡住

tomcat - JCA 的替代品

java - 将字符串错误响应转换为 Http 状态代码

version - 我需要安装JRE8吗

java - JSP中如何缩短URL地址

java - SOLR配置问题

java - 未能编译 Tomcat 的 index.jsp 示例