已解決:在CRUDServices.java中添加 @Service 注釋并在主應用程式中洗掉 @ComponentScan 解決了所有問題。
我有一個帶有 Spring Boot Web、JPA 和 PostgreSQL 資料庫的小應用程式。當嘗試呼叫這些 API 呼叫中的任何一個時,我得到一個 404。在另一個應用程式中以同樣的方式完成它是成功的。所有類都在同一個包中,我不明白為什么它找不到控制器(這似乎是大多數其他有類似/相同問題的人的問題)。
它在 8081 埠上運行,因為我在 8080 埠上運行了另一個服務。
我嘗試洗掉 ComponentScan,但隨后得到一個缺少的 bean 例外。我不明白 - 它們都在同一個包結構中?我只有一個包裹,那就是 booking.crudservice
主要應用
package booking.crudservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
@SpringBootApplication
@ComponentScan(basePackages="booking.crudservice.CRUDservices")
public class CrudserviceApplication
{
public static void main(String[] args)
{
SpringApplication.run(CrudserviceApplication.class, args);
}
}
控制器
package booking.crudservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
/*
This has PUT,POST, GET, DELETE requstest to the API endpoints for the CRUD operations
in CRUDservices.java
*/
@RestController
@RequestMapping("/bookings")
public class BookingController
{
@Autowired
private CRUDServices services;
//Return a specific booking by ID.
@RequestMapping("/{bookingID}")
public Optional<BookingEntity> getBookingByID(@PathVariable String bookingID)
{
return services.getBookingByID(bookingID);
}
//Return all bookings
@RequestMapping("/allbookings")
public List<BookingEntity> getAllBookings()
{
return services.getAllBookings();
}
@RequestMapping(method = RequestMethod.POST, value ="/addbooking")
public void addBooking(@RequestBody BookingEntity booking)
{
services.addBooking(booking);
}
//Update a given booking (with a bookingID), and replace it with new BookingEntity instance.
@RequestMapping(method = RequestMethod.PUT, value ="/bookings/{bookingID}")
public void updateBooking(@RequestBody BookingEntity booking, @PathVariable String bookingID)
{
services.updateBooking(booking, bookingID);
}
//Deletes a booking with the given bookingID
@RequestMapping(method = RequestMethod.DELETE, value ="/bookings/{bookingID}")
public void deleteBooking(@PathVariable String bookingID)
{
services.deleteBooking(bookingID);
}
}
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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>booking</groupId>
<artifactId>crudservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>crudservice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties 檔案
server.port=8081
spring.datasource.url=jdbc:postgresql://localhost:5432/bookingDB
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect
CRUDServices.java
package booking.crudservice;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/*
This service holds all CRUD operations performed on the database.
This utilizes the BookingRepository.java, which extends the CRUDrepository interface.
Using ORM via JPA all operations on the database can be easily performed here.
To connect to the web GET,PUT,POST and DELETE http requests will be made on all the operations via
BookingController, enabling the user and other possible services to interact with the database
using API endpoints.
*/
public class CRUDServices
{
@Autowired
private BookingRepository repository;
//READ all bookings from DB using GET request
public List<BookingEntity> getAllBookings()
{
List<BookingEntity> bookings = new ArrayList<>();
repository.findAll().
forEach(bookings::add);
return bookings;
}
//SEARCH for a booking, given bookingID
public Optional<BookingEntity> getBookingByID(String bookingID)
{
return repository.findById(bookingID);
}
public void addBooking(BookingEntity booking)
{
repository.save(booking);
}
public void updateBooking(BookingEntity booking, String bookingID)
{
//1. Search for the given ID in the database
//2. Replace the booking object on that ID with new booking object from parameter.
}
public void deleteBooking(String bookingID)
{
repository.deleteById(bookingID);
}
}
uj5u.com熱心網友回復:
由于您CrudserviceApplication.class存在于基礎包本身中。您不必使用 @ComponentScan 注釋。
您應該@Service在服務類 ( CRUDServices.class) 中有注釋,以便 spring 維護和注入它。
此外,您能否驗證您是否正在使用/bookings/yourhandlerendpoint前綴呼叫所有處理程式方法。在你的情況下
- /bookings/{bookingID}
- /預訂/所有預訂
- /預訂/添加預訂
- /bookings/bookings/{bookingID}
- /bookings/bookings/{bookingID}
另外,我可以看到您/bookings為幾種方法添加了兩次 perfix。
uj5u.com熱心網友回復:
在 CrudserviceApplication 類中,您提到 base-package 為
@ComponentScan(basePackages="booking.crudservice.CRUDservices")
這意味著 spring boot 將查找帶有 spring 原型注釋的類,例如
- @零件
- @配置
- @控制器
- @RestController
- @服務
對于booking.crudservice.CRUDservices包及其子包中存在的類。但是,我可以看到您的 BookingController 類存在于booking.crudservice包中,它不是子包(實際上,它是父包)。
這就是為什么 spring boot 不掃描 BooKingController 類并且你得到 404 的原因。
作為修復,您可以洗掉 @ComponentScan 注釋中的 basePackages 引數,它將掃描booking.crudservice及其子包中存在的所有組件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/445637.html
上一篇:鏈表和陣列的區別
