Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 简单的spring启动应用程序不起作用_Java_Spring Boot - Fatal编程技术网

Java 简单的spring启动应用程序不起作用

Java 简单的spring启动应用程序不起作用,java,spring-boot,Java,Spring Boot,我创建了非常简单的spring启动应用程序。但它不会返回预期的输出。我在下面提到了试用代码 主要内容: 控制器: package com.example.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.an

我创建了非常简单的spring启动应用程序。但它不会返回预期的输出。我在下面提到了试用代码

主要内容:

控制器:

package com.example.controller;

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

@RestController
@RequestMapping("/api")
public class ControllerClass {
    
    
    @GetMapping("/all")
    public String getAllUsers() {
        return "get All users";
    }

}
特性:

server.port=8090
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.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.practice</groupId>
    <artifactId>practice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Practice 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-devtools</artifactId>
            <scope>runtime</scope>
            <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>
我在代码中犯了什么错误。我是新来的
spring boot
。请帮我解决这个问题。谢谢。

试着用这个

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

@RestController
@RequestMapping("/api")
public class ControllerClass {
    
    
    @GetMapping("/all")
    public ResponseEntity<String> getAllUsers() {
        return new ResponseEntity<>("get All users",HttpStatus.OK);
    }

}
而且@SpringBootApplication无法自动扫描您的bean。有两种解决方法:

  • 将DemoApplication类移动到下一个路径com.example,这是spring引导到下面包中的autoscan bean所必需的
  • 您需要向DemoApplication添加额外的注解@ComponentScan(“com.example”)

  • 这可能是类包的问题

    确保您的
    ControllerClass
    位于
    DemoApplication
    的子包中(例如,
    DemoApplication
    位于
    org.example
    包中,控制器位于
    org.example.controllers
    包中)或使用
    @SpringBootApplication(scanBasePackages=“package.with.your.controller”)

    问题是,
    @springbootplication
    注释包括:

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    

    @ComponentScan
    这里没有参数,因此根据文档,将从声明此注释的类的包中进行扫描。它将扫描
    com.example.practice
    和子包,但不扫描
    com.example.controllers

    这些类的包名是什么?@SKumar I用包更新了问题将控制器添加到
    com.example.practice>的子包中。因此,最好使用
    com.example.practice.controller
    之类的工具,而不要使用
    com.example.controller
    。当您运行Spring启动应用程序时(即使用@SpringBootApplication注释的类),Spring将只扫描主类包下面的类。确保主类位于其他类之上的根包中。无需返回
    ResponseEntity
    。你的答案并不能解释你认为OP密码有什么问题,只是建议你用一种魔药。是的,@AbhijitSarkar你说得对,谢谢。我不明白有不同的包裹。
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/api")
    public class ControllerClass {
        
        
        @GetMapping("/all")
        public ResponseEntity<String> getAllUsers() {
            return new ResponseEntity<>("get All users",HttpStatus.OK);
        }
    
    }
    
    package com.example.practice;
    package com.example.controller;
    
    @Configuration
    @EnableAutoConfiguration
    @ComponentScan