【问题标题】:SOLVED - HTTP Status 404 for Spring Web REST API after deploying dynamic application to Tomcat server on Eclipse已解决 - 在 Eclipse 上将动态应用程序部署到 Tomcat 服务器后,Spring Web REST API 的 HTTP 状态 404
【发布时间】:2020-04-21 07:43:16
【问题描述】:

我从https://start.spring.io/ 获得了一个新的 Spring Web 应用程序,其中包含一些依赖项。 每当我尝试访问 REST 控制器路径(“api/companies”)时,服务器都会发送带有以下消息的 404 结果:

“源服务器没有找到当前的表示 目标资源或不愿透露其存在。”。

这是我的 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 控制器如下:

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 项目设置 > 上下文根将项目上下文根从默认的“公司”更改为“/”,并通过服务器模块设置更改了路径,如下所示:

我做错了什么?有没有我遗漏的配置?

【问题讨论】:

    标签: java spring eclipse rest tomcat


    【解决方案1】:

    [已解决] 修复 Eclipse 中的部署程序集路径解决了该问题。

    【讨论】:

      猜你喜欢
      • 2016-11-15
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 2014-05-12
      • 2015-10-18
      • 2012-07-02
      相关资源
      最近更新 更多