簡體   English   中英

Spring 啟動 Rest 服務返回 HTTP-404

[英]Spring Boot Rest service returns HTTP-404

我已經使用 Spring Boot 有一段時間了,但想創建一個新的應用程序,從一個簡單的 REST 服務開始。 無論我嘗試什么,我的服務都會不斷返回 404 錯誤,我無法找出原因。 I've found some similar sounding problems on the net but they almost always go about the Controller class not being in the right package, but that is NOT the case here. 為了排除端口問題,我將其從 8080->8901(和其他幾個)更改但無濟於事。 我還嘗試了 @ComponentScan ,但它不起作用。 以下是文件:

應用 class:

package nl.haba.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

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

Controller class:

package nl.haba.demo.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class GreetingService {

    @GetMapping("/greet")
    public ResponseEntity<String> greetPerson() {
        return ResponseEntity.ok().body("Hello, World");
    }
}

應用程序.yml

server:
  port: 8901

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.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>nl.haba</groupId>
    <artifactId>mydemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mydemo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </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>

Spring啟動日志:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-06-17 11:15:06.650  INFO 11251 --- [           main] n.h.demo.DemoApplication   : Starting DemoApplication on XXXXXXXXXXXXX with PID 11251 (/Users/xxxxxxxxxx/Projects/mydemo/target/classes started by xxxxxxxxxx in /Users/xxxxxxxxxx/Projects/mydemo)
2020-06-17 11:15:06.653  INFO 11251 --- [           main] n.h.demo.DemoApplication   : No active profile set, falling back to default profiles: default
2020-06-17 11:15:07.357  INFO 11251 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8901 (http)
2020-06-17 11:15:07.365  INFO 11251 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-06-17 11:15:07.366  INFO 11251 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-06-17 11:15:07.442  INFO 11251 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-06-17 11:15:07.442  INFO 11251 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 748 ms
2020-06-17 11:15:07.586  INFO 11251 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8901 (http) with context path ''
2020-06-17 11:15:07.598  INFO 11251 --- [           main] n.h.demo.DemoApplication   : Started DemoApplication in 1.282 seconds (JVM running for 1.921)

當我使用 Postman 獲得結果時

GET http://localhost:8901/greet

它總是以 404 返回,我不知道為什么。 Curl 也不起作用。 有人可以幫助我並告訴我我在這里缺少什么嗎?

謝謝大家,這成功了。 I replaced the spring-boot-starter-jersey package with the spring-boot-starter-web package and it worked fine, Since i was planning on using only rest from Spring Boot. 我認為選擇 jersey package 是一個不錯的選擇,因為 web ZEFE70A8E604A7C840D8ZF0 的負載似乎超過了所需的 3A67B。 猜猜我仍然需要了解 Spring 引導中的 package 選擇。

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM