代码之家  ›  专栏  ›  技术社区  ›  Carla

无法运行Spring引导简单REST服务

  •  0
  • Carla  · 技术社区  · 7 年前


    我正在尝试运行一个用Spring初始化器创建的基本Spring启动应用程序。我看到该应用程序与Web上的许多示例几乎相同,但我没有找到“404”页面:

    package com.example;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMethod;
    @RestController
    public class Controller {
    
            @RequestMapping(value = "/", method = RequestMethod.GET)
            public String hello() {
                    return "it works!";
    
            }
    }
    

    package com.example.springtest;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DemoApplication {
    
            public static void main(String[] args) {
                    SpringApplication.run(DemoApplication.class, args);
            }
    }
    

    pom.xml仅包括web初学者:

    <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>
            <parent>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.1.1.RELEASE</version>
                    <relativePath/> <!-- lookup parent from repository -->
            </parent>
            <groupId>com.example</groupId>
            <artifactId>spring-test</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <packaging>jar</packaging>
            <name>demo</name>
            <description>Demo project for Spring Boot</description>
    
            <properties>
                    <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>
            </dependencies>
    
            <build>
                    <plugins>
                            <plugin>
                                    <groupId>org.springframework.boot</groupId>
                                    <artifactId>spring-boot-maven-plugin</artifactId>
                            </plugin>
                    </plugins>
            </build>
    
    
    </project>
    

    $ curl http://localhost:8080
    
    {"timestamp":"2018-12-11T09:18:59.236+0000","status":404,"error":"Not Found","message":"No message available","path":"/"}
    

    我是否错过了让它工作的启动程序?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Sunny Goel    7 年前

    使用 @ComponentScan() 注释,并将路径作为参数传递给rest服务组件扫描。

    因为默认情况下,它只包括主类的包和子包中的服务。因此,要添加其他路径服务,必须在组件扫描注释中传递路径。