【问题标题】:Spring boot always send error on localhost:8080Spring Boot 总是在 localhost:8080 上发送错误
【发布时间】:2021-06-29 23:33:29
【问题描述】:

我有用户存储库

    package registry;
    import Entity.User;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;

    @Repository
    public interface UserRepository extends JpaRepository<User, Long> {}

用户控制器

package controller;

import Entity.User;
import exception.NoUserFoundException;
import exception.UserNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import registry.UserRepository;
import service.UserService;

import javax.xml.ws.Response;
import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:4200")
@SpringBootApplication(scanBasePackages = {"controller", "service","registry"})
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/getall")
    public ResponseEntity<?> getAllUsers() {
        return new ResponseEntity<>(userService.getAllUsers(), HttpStatus.OK);
    }
}

应用程序运行器

package Application;

import Entity.User;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import registry.UserRepository;
import service.UserService;
import service.UserServiceImpl;
import controller.UserController;

@SpringBootApplication
public class ApplicationRunner  {
    public static void main(String... args) {
        SpringApplication.run(ApplicationRunner.class, args);
    }

}

应用程序运行良好。 当我输入网址“localhost://8080”或“localhost://8080/user/getall”时,我收到以下错误消息:

白标错误页面

此应用程序没有显式映射 /error,因此您将其视为后备。

2021 年 6 月 30 日星期三 01:30:30 CEST

出现意外错误(类型=未找到,状态=404)。

任何帮助表示赞赏

【问题讨论】:

  • localhost://8080 不是有效的 URL,请改用 localhost:8080/。但是localhost:8080 不存在,所以你会得到一个404。另外请从界面中删除@Repository(它不做任何事情)并从你的控制器中删除@SpringBootApplication。接下来你的结构是错误的,你的 ApplicationRunner 应该在一个覆盖你其他包的包中(所以你的其他包应该是 Application.controllerApplication.registry

标签: spring-boot controller


【解决方案1】:

URL 应该类似于 HOST:PORT 使用网址:http://localhost:8080/user/getall

【讨论】:

    【解决方案2】:

    对不起,这是一个错字。 网址是 http://localhost:8080/user/getall

    问题依旧

    【讨论】:

      猜你喜欢
      • 2018-01-07
      • 1970-01-01
      • 2018-08-07
      • 2020-05-08
      • 2021-11-15
      • 2018-07-15
      • 1970-01-01
      • 2014-02-21
      • 2021-06-28
      相关资源
      最近更新 更多