这是我第一次在Spring Boot 时创建API,我正在尝试创建事务API。运行应用程序时,我收到此错误**说明:***TransactionService.transactionService.modal.TransactionRequest中构造函数的参数0需要一个类型为'int'的Bean,但找不到该Bean。***操作:**考虑在配置中定义类型为'int'的Bean。
莫代尔 Package :交易实体
@Getter
@Setter
@Builder
@Entity
public class TransactionEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int transactionId;
@NotNull
@Column(unique = true)
private UUID externalId;
@NotNull
private int userId;
@NotNull
private int merchantId;
@NotNull
private int clientReferenceId;
@NotNull
private double amount;
@Enumerated(EnumType.STRING)
@NotNull
private TransactionStatus status;
@NotNull
private String createdBy;
private String updatedBy;
@NotNull
private LocalDateTime createdAt;
@NotNull
private LocalDateTime updatedAt;
}
交易请求
@Component
@Data
@Builder
public class TransactionRequest {
private int userId;
private int merchantId;
private int clientReferenceId;
private double amount;
private String createdBy;
}
交易响应
@Component
@Data
@Builder
public class TransactionResponse {
private int userId;
private int merchantId;
private int clientReferenceId;
private double amount;
private LocalDateTime createdAt;
private TransactionStatus status;
}
交易道
@Component
// Dao class
public class TransactionDao {
@Autowired
TransactionRepository transactionRepository;
TransactionEntity transactionEntity;
public TransactionResponse createTransaction(TransactionRequest transactionRequest){
LocalDateTime cuurentTime = LocalDateTime.now();
transactionEntity.builder().userId(transactionRequest.getUserId())
.merchantId(transactionRequest.getMerchantId())
.clientReferenceId(transactionRequest.getClientReferenceId())
.amount(transactionRequest.getAmount())
.createdBy(transactionRequest.getCreatedBy())
.createdAt(cuurentTime)
.updatedAt(cuurentTime)
.externalId(UUID.randomUUID())
.status(TransactionStatus.CREATED);
transactionRepository.save(transactionEntity);
return TransactionResponse.builder().status(transactionEntity.getStatus())
.createdAt(transactionEntity.getCreatedAt()).build();
}
}
交易服务
@Service
public class TransactoinService {
@Autowired
public TransactionDao transactionDao;
public TransactionResponse createTransaction(TransactionRequest transactionRequest){
return transactionDao.createTransaction(transactionRequest);
}
}
交易控制器
@RestController
public class TransactionController {
@Autowired
TransactoinService transactoinService;
@PostMapping
TransactionResponse createTransaction(@RequestBody TransactionRequest transactionRequest){
return transactoinService.createTransaction(transactionRequest);
}
}
2条答案
按热度按时间lkaoscv71#
TransactionRequest
被注解为@Component
,因此spring boot
自动扫描将尝试在该类之外创建@Bean
。它还使用
@Data
进行了注解,因此在创建bean
时,Spring boot
尝试将inject
其他bean作为参数插入所有参数constructor
,并且它没有找到要注入constructor
的“int”bean
。我猜事务响应不应该是
@Component
,或者至少不应该是Singleton
bean。ecfdbz9o2#
您不应将POJO类创建为Spring Bean。请删除
TransactionRequest
和TransactionResponse
POJO类中的@Component
注解。