我有以下問題。我有一個簡單的Spring Boot應用程式,它應該用來處理用戶的債務。該服務確實可以編譯,但無論是插入新記錄的PostRequest還是顯示用戶債務的GetRequest都不起作用。當服務啟動時,應用程式會自動為該物體創建適當的表。因此,我認為資料庫連接不會是問題。看起來我的服務不能識別RestController。使用Postman,我一直得到錯誤代碼 "404 Not Found"。
。有誰知道我做錯了什么嗎?
我的物體
package Entity。
import javax.persistence.Column;
import javax.persistence.Entity。
import javax.persistence.GeneratedValue。
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table。
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@Entity.
@Table(name= "debts")
@EnableAutoConfiguration(name="debts")
public class DebtsEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "user_id")
private String userId。
@Column(name = "invoice_id")
private int invoiceId ;
@Column(name = "creditor")
private String creditor;
@Column(name = "amount")
private double amount;
@Column(name = "deadline")
private String deadline。
@Column(name = "installment")
private double installment;
public DebtsEntity(){
}
public DebtsEntity(int id, String userId, int invoiceId, String creditor, double amount, String deadline,
double installment) {
super()。
this.id = id;
this.userId = userId。
this.invoiceId = invoiceId。
this.creditor = creditor;
this.amount = amount;
this.deadline = deadline;
this.installment = installment;
}
public int getId(/span>) {
return id。
}
public void setId(int id) {
this.id = id;
}
public String getUserId() {
return userId。
}
public void setUserId(String userId) {
this.userId = userId。
}
public int getInvoiceId(/span>) {
return invoiceId。
}
public void setInvoiceId(int invoiceId) {
this.invoiceId = invoiceId。
}
public String getCreditor() {
return債權人。
}
public void setCreditor(String creditor) {
this.creditor = creditor;
}
public double getAmount(/span>) {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getDeadline() {
return deadline。
}
public void setDeadline(String deadline)。{
this.deadline = deadline。
}
public double getInstallment(/span>) {
return installment;
}
public void setInstallment(double installment) {
this.installment = installment。
}
@Override.
public String toString(){
return "debtsEntity [id="/span> id "。userId=" userId ", invoiceId=" invoiceId ", creditor=" creditor
", amount=" amount ", deadline=" deadline ", installment=" installment "] ;
}
我的RestController
package Controller;
import java.net.URI。
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired。
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable。
import org.springframework.web.bind.annotation.PostMapping。
import org.springframework.web.bind.annotation.RequestBody。
import org.springframework.web.bind.annotation.RestController。
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import DAO.DebtsDAO;
import Entity.DebtsEntity;
{
債務DAO debtsDAO。
public List<DebtsEntity> retrieveDebtsByUserId( String userId)
{
List<DebtsEntity> debts = debtsDAO.findByUserId(userId)。
return debts;
}
我的DAO
package DAO。
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component。
import Entity.DebtsEntity;
@Component
public interface DebtsDAO extends JpaRepository< DebtsEntity, Integer>{
List<DebtsEntity>findByUserId(String user_id)。
我的application.properties
##資料庫配置。
spring.datasource.url=jdbc:postgresql://localhost:5432/Test
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=create
# define if Database Queries should be written in logfile[/span].
spring.jpa.show-sql=true。
spring.jpa.properties.hibernate.default_schema=public
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect
server.port=8000。
spring.application.name=Debts
我的依賴性
<?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"/span>>
<modelVersion>/span>4.0.0</modelVersion>/span>
<parent>/span>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>/span>2.5.4</version>
<relativePath/> <!--從版本庫中查找父本--> < >
</parent>
<groupId>LifeOps</groupId>/span>
<artifactId>Service</artifactId>
<version>0.0.1-SNAPSHOT</version>/span>
<name>Service</name>/span>
<description>Spring Boot的示范專案</description>
<properties>/span>
<java.version>11</java.version>/span>
</properties>/span>
<dependencies>>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>/span>
<!--https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>/span>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>/span>
<!--https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>/span>org.postgresql</groupId>
<artifactId>/span>postgresql</artifactId>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</依賴性>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>/span>
</dependencies>
<build>/span>
<plugins>/span>
<plugin>/span>
<groupId>org.springframework.boot</groupId>/span>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>/span>
<excludes>/span>
</excludes>/span>
</configuration>/span>
</plugin>/span>
</plugins>/span>
</build>/span>
</project>
UPDATE 1
Get-Request
URL: http://localhost:8000/1000/depts
PostRequest
URL: http://localhost:8000/depts RequestBody:
{<
"id":1000,
"user_id": "ABC1234",
"invoice_id":100001,
"創建者": "A12L",
"金額": 123012.56,
"截止日期": "20.10.12",
"分期付款": 50.00
}
package Service。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication。
import org.springframework.boot。 autoconfigure.domain.EntityScan。
@EntityScan(basePackages = "物體")
@SpringBootApplication(basePackages = "entity")
public class ServiceApplication {
public static void main(span class="hljs-built_in">String[] args) {
SpringApplication.run(ServiceApplication.class, args) 。
}
更新2
Class package
DebtsApplication service.debts
DebtsController service.debts.controller
DebtsEntity service.debts.entity。
DebtsDAO service.debts.dao
uj5u.com熱心網友回復:
我建議你添加一個基礎包(類似于 "com.doncarlito")并將ServiceApplication放在其中。
package com.doncarlito;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
@EntityScan(basePackages = "entity")
@SpringBootApplication
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args)。
}
}
然后將所有其他存在的包轉移到這個包的子包中:
原因是@SpringBootApplication封裝了@Configuration、@EnableAutoConfiguration和@ComponentScan注釋的默認屬性。@ComponentScan的默認值意味著所有使用@ComponentScan的包上的子包都被掃描。這就是為什么在專案的基礎包中包含主類通常是一個好的做法。
uj5u.com熱心網友回復:
你從x.y.z.ApplicationService運行你的應用程式,但你的組件在不同的包中,如x.y.a.YourComponent,這將不會被spring拾取。
默認情況下,spring會掃描你的@SpringApplication類的包(和后裔)。參照上面的內容,它將是x.y.z.*.....
為了解決你的問題(替代方案)
將你的ApplicationService移到DebtsApplication旁邊,這樣它就會在所有需要的組件(例如@RestController)的 "上面"。
@ComponentScan(basePackgages={'service.debts'})ApplicationService,這樣你就可以 "手動 "顯示Spring尋找組件的位置了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/317517.html
標籤:
上一篇:與JPARepository一起使用的QueryDSL謂詞,其中欄位是使用AttributeConverter轉換為List<Object>的JSON字串。

