简体   繁体   English

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

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

I am developing a springboot REST application with Eclipse, but I have a problem deploying it to Tomcat 10.0.22.我正在使用 Eclipse 开发一个 springboot REST 应用程序,但是将它部署到 Tomcat 10.0.22 时遇到问题。 When I run my application from the IDE the endpoints are working fine, they returns me the correct JSONs but when I try to access them from my server they throw a 404 error.当我从 IDE 运行我的应用程序时,端点工作正常,它们会返回正确的 JSON,但是当我尝试从服务器访问它们时,它们会抛出 404 错误。 I am packaging it as war.我把它包装成战争。 I have created the project from https://start.spring.io/ setting Spring web dependency, java 18 and packaging war.我已经从https://start.spring.io/设置 Spring web 依赖项、java 18 和打包大战创建了项目。 I imported it to eclipse as an existing maven project.我将它作为现有的 Maven 项目导入到 Eclipse 中。 To be sure that I deployed it correctly I had created a jsp file and load it as index.jsp and it is showing correctly on the deployed server and the IDE.为了确保我正确部署它,我创建了一个 jsp 文件并将其加载为 index.jsp,它在部署的服务器和 IDE 上正确显示。 I have tried to restart the proyect, change the java version in both, export the proyect as jar, to deploy it on another tomcat version, to extend SpringBootServletInitializer from the main class and override the confirm method, without the method implemented and without extending it from anywhere.我尝试重新启动项目,更改两者的java版本,将项目导出为jar,将其部署在另一个tomcat版本上,从主类扩展SpringBootServletInitializer并覆盖confirm方法,没有实现该方法并且没有扩展它从任何地方。 I don't understand why when I deploy it to the Tomcat server from the Tomcat manager the endpoint calls are throwing a 404 error.我不明白为什么当我从 Tomcat 管理器将其部署到 Tomcat 服务器时,端点调用会引发 404 错误。 I think that is possible that Tomcat is not loading Springboot libraries, because I do not see the console Spring initializing message when I start the server from console, but I do not know how to solve this or if it is what is happening.我认为 Tomcat 可能没有加载 Springboot 库,因为当我从控制台启动服务器时,我没有看到控制台 Spring 初始化消息,但我不知道如何解决这个问题,或者它是否正在发生。 Can anyone help me please.谁能帮助我。

This is my main class, TestApplication.java:这是我的主要课程,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);
    }
}

This is my servlet initializer application, ServletInitializer.java:这是我的 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);
    }
}

This is my controller, UserController.java:这是我的控制器,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);      
    }   
}

This is my pom.xml:这是我的 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>

The mentioned index.jsp:提到的 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>

EDIT: Because I have had a questions about the structure below this I am appending a picture of it.编辑:因为我对下面的结构有疑问,所以我附上了它的图片。 Project folder structure项目文件夹结构

Just to be sure have you put your JSP files under web-content and not under WEB-INF because in Eclipse the files are not accessed there by the server.只是为了确保您将 JSP 文件放在 web-content 下而不是 WEB-INF 下,因为在 Eclipse 中,服务器不会访问这些文件。 And one more thing try to check server location and see whether Tomcat is installed in the correct server location and it is showing in your Eclipse,if not try changing your server location to the location where Tomcat is installed and restart your server.还有一件事尝试检查服务器位置并查看 Tomcat 是否安装在正确的服务器位置并显示在您的 Eclipse 中,如果没有尝试将服务器位置更改为安装 Tomcat 的位置并重新启动服务器。

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

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