我从 https://start.spring.io/ 获得了一个新的 Spring Web 应用程序有一些依赖。 每当我尝试访问 REST Controller 路径(“api/companies”)时,服务器都会发送带有以下消息的 404 结果:
"The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.".
这是我的 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.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>helium.erp.com</groupId>
<artifactId>companies</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>companies</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
项目结构如下:
应用程序的入口点是 CompaniesApplication 类:
package helium.erp.com.companies;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class CompaniesApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(CompaniesApplication.class, args);
}
}
我尝试访问的 REST Controller 如下:
package helium.erp.com.companies.company;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/companies")
public class CompanyApi {
@GetMapping(value = "")
public ResponseEntity<String> getAllCompanies() {
return ResponseEntity.ok("all companies");
}
}
我在 WebContent 的根目录中放置了一个 html 文件,当我向“localhost:9091”发送 GET 时,它会完美返回。该项目被添加为服务器资源,以下是服务器的配置:
我已通过 Eclipse 项目设置 > Web 项目设置 > 上下文根将项目上下文根从默认的“公司”更改为“/”,并通过服务器模块设置更改了路径,如下所示:
我做错了什么?我是否缺少某些配置?
最佳答案
[已解决]修复 Eclipse 中的部署程序集路径解决了该问题。
关于java - 将动态应用程序部署到 Eclipse 上的 Tomcat 服务器后,Spring Web REST API 的 HTTP 状态 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61338350/