【发布时间】: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.controller和Application.registry。