为什么找不到 localhost:8080 (404)

问题描述 投票:0回答:1

开始使用 Intellij 中的 Gradle 和 Springboot 制作应用程序。当我运行应用程序时,由于某种原因,地址

localhost:8080/projectName 

响应正确,但是

localhost:8080 

或任何

localhost:8080/somePage 

未找到(404)

可能是什么问题,我该如何解决?

我添加了

server.servlet.context-path=/ 

到我的 application.properties 文件,但没有任何改变

这是我正在使用的 RestController:

@RestController
public class OrderController {

private final OrderRepository repository;

public OrderController(OrderRepository repository) {
    this.repository = repository;
}

@GetMapping("/")
public String root() {
    return "Welcome to the root URL!";
}

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

@GetMapping("api/orders")
public Iterable<Order> getOrders() {
    return repository.findAll();
}

@PostMapping("api/orders")
public Order saveOrder(@Valid @RequestBody Order order) {
    return repository.save(order);
}

@GetMapping("api/orders/{id}")
public Order getOrders(@PathVariable Long id) {
  return repository.findById(id).orElseThrow(() -> new 
ResponseStatusException(HttpStatus.NOT_FOUND));
}


@ExceptionHandler
public ResponseEntity<Void> handle(ResponseStatusException e) {
  return new ResponseEntity<>(null, e.getStatusCode());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void 
handleValidationExceptions(MethodArgumentNotValidException ex) {
}
}
java spring-boot gradle intellij-idea localhost
1个回答
0
投票

您必须创建一个默认构造函数。如果没有它,您的控制器就不会被实例化。

public OrderController() {}
 
© www.soinside.com 2019 - 2024. All rights reserved.