Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导应用程序未在外部Tomcat中启动(在Tomcat 8.5和10中都尝试过)_Java_Spring Boot_Maven_Tomcat - Fatal编程技术网

Java Spring引导应用程序未在外部Tomcat中启动(在Tomcat 8.5和10中都尝试过)

Java Spring引导应用程序未在外部Tomcat中启动(在Tomcat 8.5和10中都尝试过),java,spring-boot,maven,tomcat,Java,Spring Boot,Maven,Tomcat,我正在尝试在外部tomcat中部署一个rest示例。然而,它不会给出任何错误,Spring也不会启动。根据Spring文档,我遵循了三个步骤 在pom.xml中打包为war 提供给pom.xml中的spring boot starter tomcat 使用SpringBootServletInitializer扩展主类 当我将其放在Tomcat的webapp文件夹中并运行“catalina.bat run”时,它表示应用程序已成功部署,但无论我尝试访问哪个URL,它都不会执行rest方法 @Sp

我正在尝试在外部tomcat中部署一个rest示例。然而,它不会给出任何错误,Spring也不会启动。根据Spring文档,我遵循了三个步骤

  • 在pom.xml中打包为war
  • 提供给pom.xml中的spring boot starter tomcat
  • 使用SpringBootServletInitializer扩展主类
  • 当我将其放在Tomcat的webapp文件夹中并运行“catalina.bat run”时,它表示应用程序已成功部署,但无论我尝试访问哪个URL,它都不会执行rest方法

    @SpringBootApplication
    @RestController
    public class HelloWorldApplication extends SpringBootServletInitializer
    {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
        {
            return application.sources(HelloWorldApplication.class);
        }
        public static void main(String[] args)
        {
            SpringApplication.run(HelloWorldApplication.class, args);
        }
        @RequestMapping(value = "/hello")
        public String helloWorld()
        {
            return "Hello World, Peter";
        }
    }
    
    在pom.xml中

    
    org.springframework.boot
    弹簧启动机tomcat
    假如
    com.example
    你好世界
    0.0.1-快照
    你好世界
    SpringBoot的演示项目
    战争
    
    而application.properties类似于

    server.servlet.context-path=/sample
    
    Tomcat日志:

        15-Mar-2020 23:06:46.219 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\softwares\apache-tomcat-10.0.0-M1\webapps\HelloWorld-0.0.1-SNAPSHOT.war]
        15-Mar-2020 23:06:48.102 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
        15-Mar-2020 23:06:48.409 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [270] milliseconds.
        15-Mar-2020 23:06:48.440 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\softwares\apache-tomcat-10.0.0-M1\webapps\HelloWorld-0.0.1-SNAPSHOT.war] has finished in [2,221] ms
    
    我试着用下面的方法访问,但没有一种有效。有人能给我指出正确的方向吗

    localhost:8080/HelloWorld-0.0.1-SNAPSHOT
    localhost:8080/HelloWorld-0.0.1-SNAPSHOT/hello
    localhost:8080/HelloWorld-0.0.1-SNAPSHOT/HelloWorldApplication/helloWorld
    

    您的路径是在此函数中设置的

    @RequestMapping(value = "/hello")
    public String helloWorld()
    {
        return "Hello World, Peter";
    }
    
    这意味着您只能访问
    “/hello”
    。 访问
    localhost:8080/HelloWorld-0.0.1-SNAPSHOT
    将不起作用,您必须访问
    localhost:8080/${context path}/${your path}
    在这种情况下
    localhost:8080/sample/hello
    试试这个方法

    localhost:8080/app name/hello

    在pom.xml中

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>it.group.id</groupId>
        <artifactId>artifactId</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath/> <!-- lookup parent fdaotory -->
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
    
            <!--For Build War File-->
            <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-tomcat</artifactId>
                 <scope>provided</scope>
            </dependency>
    
    
    
        </dependencies>
        <build>
            <!--DEV-->
            <finalName>app-name</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
    
                <!--For Build War File-->
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
    
            </plugins>
    
        </build>
    
    </project>
    
    
    4.0.0
    it.group.id
    人工的
    1.0-快照
    战争
    org.springframework.boot
    spring启动程序父级
    2.1.5.1发布
    UTF-8
    UTF-8
    1.8
    org.springframework.boot
    弹簧启动机tomcat
    假如
    应用程序名称
    org.springframework.boot
    springbootmaven插件
    maven战争插件
    3.2.2
    假的
    
    尝试使用
    localhost:8080/sample/hello
    当我在嵌入式服务器上访问它时,它可以工作,但在外部tomcat上它不工作。这是否回答了您的问题?据我所知,您无法在Tomcat 10上运行Spring Boot,因为Tomcat 10使用了新的Jakarta EE命名空间(
    Jakarta.
    ),而不是旧的Java EE命名空间(
    javax.
    ),另请参阅。所以不要试图在Tomcat 10上运行Spring Boot,使用Tomcat 9或8.5。当我在嵌入式服务器上访问它时,它可以工作,但在外部Tomcat上它不工作