我想知道如何從我的物體模型類訪問一個屬性來執行一個條件,我需要status在存盤庫函式執行洗掉之前驗證資料庫中的列是 0 還是 2 deleteById(),就好像它是 1,它應該不被執行。
我的想法是將業務登錄放在服務層,所以我試圖把條件放在那里:
public void delete(int id) {
if (reportRequest.getStatus() != 1) {
log.info("The report request {} was successfully deleted", id);
reportRequestRepository.deleteById(id);
} else {
log.error("It was not possible to delete the selected report as it hasn't been processed yet");
}
}
但我收到以下錯誤:
Error starting ApplicationContext. To display the conditions report re-run your
application with 'debug' enabled.
2022-10-25 18:44:38.538 ERROR 2328 --- [ main]
o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.ssc.test.cb3.service.ReportRequestService required a
bean of type 'com.ssc.test.cb3.model.ReportRequest' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.ssc.test.cb3.model.ReportRequest' in your
configuration.
2022-10-25 18:44:38.543 ERROR 2328 --- [ main]
o.s.test.context.TestContextManager : Caught exception while allowing
TestExecutionListener
[org.springframework.test.context.web.ServletTestExecutionListener@7d446ed1] to prepare
test instance [com.ssc.test.cb3.Cb3ApplicationTests@1f72fbd1]
java.lang.IllegalStateException: Failed to load ApplicationContext
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.735 s <<< FAILURE! -
in com.ssc.test.cb3.Cb3ApplicationTests
contextLoads Time elapsed: 0.001 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'reportRequestController': Unsatisfied dependency expressed
through field 'reportRequestService'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean
with name 'reportRequestService' defined in file
在錯誤的最后,我附上了一張圖片,因為我無法將它粘貼到這里:
錯誤跟蹤器
No qualifying bean of type 'com.ssc.test.cb3.model.ReportRequest' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
所以這是我當前的代碼:
模型類 ReportRequest:
package com.ssc.test.cb3.model;
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 lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Class that models the entity report request table of the database
* @author ssc
*/
@Entity
@Table(name = "report_request")
@Data
public class ReportRequest {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "seq_id")
private int id;
@Column(name = "request_date", insertable = false, nullable = true)
private String requestDate;
@Column(name = "request_userid")
private int requestUserId;
@Column(name = "request_email")
private String requestEmail;
@Column(name = "start_date")
private String startDate;
@Column(name = "end_date")
private String endDate;
@Column(name = "report_type")
private int reportType; // 0 === cliente, 1 === proveedor
@Column(name = "contact_id")
private int contactId;
@Column(name = "contact_id_customer") // Id from the client or provider chosen
private int contactIdCustomer;
@Column(name = "contact_id_provider") // Id from the provider or provider chosen
private int contactIdProvider;
private String rgids;
private int status; // 0 === active, 1 === inactive
@Column(name = "process_start")
private String processStart;
@Column(name = "process_finish")
private String processFinish;
@Column(name = "call_filter") // 0 === Answered calls, 1 === Not answered, 2 === both
private int callQuery;
@Column(name = "file_name")
private String fileName;
}
存盤庫類:
package com.ssc.test.cb3.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.ssc.test.cb3.model.ReportRequest;
/**
* Class that extends to the repository for database management queries with
* table report_request
*
* @author ssc
*/
@Repository
public interface ReportRequestRepository extends JpaRepository<ReportRequest, Integer> {
}
服務等級:
package com.ssc.test.cb3.service;
import com.ssc.test.cb3.repository.ReportRequestRepository;
import org.springframework.stereotype.Service;
import com.ssc.test.cb3.model.ReportRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Class to prepare the services to be dispatched upon request.
*
* @author ssc
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class ReportRequestService {
@Autowired
private ReportRequest reportRequest;
private final ReportRequestRepository reportRequestRepository;
/**
* Function to delete a report from the database
*
* @param id from the report request objet to identify what is the specific
* report
*/
// public void delete(int id) {
// log.info("The report request {} was successfully deleted", id);
// reportRequestRepository.deleteById(id);
// }
public void delete(int id) {
if (reportRequest.getStatus() != 1) {
log.info("The report request {} was successfully deleted", id);
reportRequestRepository.deleteById(id);
} else {
log.error("It was not possible to delete the selected report as it hasn't been processed yet");
}
}
}
類控制器:
package com.ssc.test.cb3.controller;
import com.ssc.test.cb3.model.ReportRequest;
import com.ssc.test.cb3.service.ReportRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Class to handle REST services and APIs for the download Report's class
*
* @author ssc
*/
@RestController
@RequestMapping("/v1/reportRequest")
@CrossOrigin(origins = "http://localhost:3000")
public class ReportRequestController {
@Autowired
private ReportRequestService reportRequestService;
/**
* Method to delete a report request by its id number, it will call the service method and passed the id captured
* @param id
*/
@DeleteMapping("/delete/{id}")
public void deleteReport(@PathVariable int id) {
reportRequestService.delete(id);
}
}
如果有人能指導我如何完成這項任務,我將不勝感激。
uj5u.com熱心網友回復:
您正在嘗試將物體類注入服務類
@Autowired
private ReportRequest reportRequest;
那條線是例外的,因為該物體不是由 Spring 管理的。
我猜您正在嘗試獲取特定記錄,這樣您就必須使用存盤庫。
嘗試這個...
public void delete(int id) {
ReportRequest reportRequest = reportRequestRepository.findById(id).orElse(null);
if (reportRequest == null || reportRequest.getStatus() == 1) {
log.error("It was not possible to delete the selected report as it hasn't
been processed yet");
} else {
reportRequestRepository.deleteById(id);
log.info("The report request {} was successfully deleted", id);
}
}
我還建議您拋出例外并使用@ControllerAdvice 處理要發送到前端的例外。
uj5u.com熱心網友回復:
嘗試在您的@Configuration 類/SpringBoot 啟動器中添加(可能會錯過)@ComponentScan。例如 :
@Configuration
@ComponentScan("com.my.package")
public class MyRootConfigurationClass
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/520507.html
標籤:爪哇春天弹簧靴api
