简体   繁体   English

Spring 启动 REST API 404 错误

[英]Spring boot REST API 404 error

I am trying to create a basic spring boot application (JDK 1.8) with a REST API .我正在尝试使用 REST API 创建一个基本的 Spring Boot 应用程序(JDK 1.8)。 The following is my application code以下是我的应用程序代码

package org.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("org.demo")
@EnableAutoConfiguration
public class DemoApplication {

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

I have added a simple controller as follows我添加了一个简单的控制器如下

    package org.demo.controllers;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController(value = "/hello")
public class HelloController {

    @GetMapping
    @ResponseBody
    public String hello() {
        return "Hello";
    }
}

The POM file of my project is as follows我项目的POM文件如下

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

    </dependencies>

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


</project>

When I run this application, it starts successfully.当我运行此应用程序时,它成功启动。 However, when I try to hit the URL localhost:8080 or loclahost:8080/hello, I get the 404 error message但是,当我尝试访问 URL localhost:8080 或 loclahost:8080/hello 时,我收到 404 错误消息

The following are the startup logs :: Spring Boot :: (v2.0.1.RELEASE)以下是启动日志:: Spring Boot :: (v2.0.1.RELEASE)

2018-04-24 21:51:38.888  INFO 12068 --- [           main] org.demo.DemoApplication                 : Starting DemoApplication on DESKTOP-G2QR23G with PID 12068 (I:\demo\spring-boot\demo\target\classes started by chirayu in I:\demo\spring-boot\demo)
2018-04-24 21:51:38.892  INFO 12068 --- [           main] org.demo.DemoApplication                 : No active profile set, falling back to default profiles: default
2018-04-24 21:51:38.970  INFO 12068 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5745ca0e: startup date [Tue Apr 24 21:51:38 IST 2018]; root of context hierarchy
2018-04-24 21:51:39.617  INFO 12068 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-04-24 21:51:39.999  INFO 12068 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-04-24 21:51:40.030  INFO 12068 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-24 21:51:40.030  INFO 12068 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-24 21:51:40.043  INFO 12068 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [I:\Program Files\Java\jdk1.8.0_66\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;I:\Program Files\Java\jdk1.8.0_66\bin;I:\Program Files\nodejs\;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\Program Files (x86)\Skype\Phone\;I:\MinGW\bin;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 ^& MySQL Utilities 1.5\;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 ^& MySQL Utilities 1.5\Doctrine extensions for PHP\;I:\Program Files (x86)\maven\apache-maven-3.3.9\bin\;I:\Program Files (x86)\Heroku\bin;C:\Program Files (x86)\git\cmd;I:\Program Files\Git\cmd;I:\Program Files\PostgreSQL\9.6\bin;I:\RailsInstaller\Git\cmd;I:\RailsInstaller\Ruby2.2.0\bin;I:\Program Files (x86)\Python\Scripts\;I:\Program Files (x86)\Python\;C:\Users\chirayu\AppData\Roaming\npm;C:\Users\chirayu\AppData\Local\Microsoft\WindowsApps;I:\Program Files (x86)\Microsoft VS Code\bin;;.]
2018-04-24 21:51:40.164  INFO 12068 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-24 21:51:40.164  INFO 12068 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1198 ms
2018-04-24 21:51:40.215  INFO 12068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-24 21:51:40.598  INFO 12068 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-24 21:51:40.633  INFO 12068 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-04-24 21:51:40.638  INFO 12068 --- [           main] org.demo.DemoApplication                 : Started DemoApplication in 2.2 seconds (JVM running for 2.797)

I am not sure what I might be missing.我不确定我可能会错过什么。 Please help.请帮忙。

I got the solution.我得到了解决方案。 It was due to package visibility.这是由于包可见性。 Main class was not able to found package in which controller class is present.主类无法找到控制器类所在的包。 So added all classes under same package.所以在同一个包下添加了所有类。 You can also place application class one level up.您还可以将应用程序类上一级。 Got help from below link:从以下链接获得帮助:

Spring Boot: Cannot access REST Controller on localhost (404) Spring Boot:无法访问本地主机上的 REST 控制器 (404)

Test as follow... did the trick for me remove the path from the notation @RestController then add the path @GetMapping notation as follow @GetMapping("/hello") test http://localhost:8080/hello you should get a correct response in order to have a response for this request http://localhost:8080/hello/hello need to add the follow:测试如下...对我来说,从@RestController 符号中删除路径然后添加路径@GetMapping 符号如下@GetMapping("/hello") test http://localhost:8080/hello 你应该得到一个正确响应 为了对这个请求做出响应 http://localhost:8080/hello/hello 需要添加以下内容:

@RestController
@RequestMapping(value = "/hello")

change to :改成 :

@RestController
@RequestMapping(value = "hello-controller")
public class HelloController {

@GetMapping(value="hello")
@ResponseBody
public String hello() {
    return "Hello";
}
}

so you can use your api from : localhost:8080/hello-controller/hello所以你可以使用你的api: localhost:8080/hello-controller/hello

  1. value in @RequestMapping below @RestController is define your class address. @RestController下面的@RequestMapping中的@RestController是定义您的类地址。
  2. value in @GetMapping is for your API address. @GetMapping值用于您的 API 地址。

Hope this explanation help you.希望这个解释对你有帮助。

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

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