繁体   English   中英

从部署在 Tomcat 上的 Springboot 应用程序访问 REST 端点时出现问题

[英]Problems accessing REST endpoints from Springboot application deployed on Tomcat

我正在使用 Eclipse 开发一个 springboot REST 应用程序,但是将它部署到 Tomcat 10.0.22 时遇到问题。 当我从 IDE 运行我的应用程序时,端点工作正常,它们会返回正确的 JSON,但是当我尝试从服务器访问它们时,它们会抛出 404 错误。 我把它包装成战争。 我已经从https://start.spring.io/设置 Spring web 依赖项、java 18 和打包大战创建了项目。 我将它作为现有的 Maven 项目导入到 Eclipse 中。 为了确保我正确部署它,我创建了一个 jsp 文件并将其加载为 index.jsp,它在部署的服务器和 IDE 上正确显示。 我尝试重新启动项目,更改两者的java版本,将项目导出为jar,将其部署在另一个tomcat版本上,从主类扩展SpringBootServletInitializer并覆盖confirm方法,没有实现该方法并且没有扩展它从任何地方。 我不明白为什么当我从 Tomcat 管理器将其部署到 Tomcat 服务器时,端点调用会引发 404 错误。 我认为 Tomcat 可能没有加载 Springboot 库,因为当我从控制台启动服务器时,我没有看到控制台 Spring 初始化消息,但我不知道如何解决这个问题,或者它是否正在发生。 谁能帮助我。

这是我的主要课程,TestApplication.java:

package com.testing.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

这是我的 servlet 初始化程序应用程序 ServletInitializer.java:

package com.testing.test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(TestApplication.class);
    }
}

这是我的控制器,UserController.java:

package com.testing.test.controllers;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    @PostMapping("/test/User")
    public String user(@RequestParam(name="id", defaultValue = "NO_ID_RECEIVED") String id) {
        return String.format("The user ID is: %s",id);      
    }   
}

这是我的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.testing</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>18</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

提到的 index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Let's see if it deploys</h2>
</body>
</html>

编辑:因为我对下面的结构有疑问,所以我附上了它的图片。 项目文件夹结构

只是为了确保您将 JSP 文件放在 web-content 下而不是 WEB-INF 下,因为在 Eclipse 中,服务器不会访问这些文件。 还有一件事尝试检查服务器位置并查看 Tomcat 是否安装在正确的服务器位置并显示在您的 Eclipse 中,如果没有尝试将服务器位置更改为安装 Tomcat 的位置并重新启动服务器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM