its my first time crating api in spring boot, i'm trying to create transaction api. when i'm running the application i'm getting this error Description: Parameter 0 of constructor in TransactionService.transactionService.modal.TransactionRequest required a bean of type 'int' that could not be found. Action: Consider defining a bean of type 'int' in your configuration.
Modal package: TransactionEntity
@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;
}
TransactionRequest
@Component
@Data
@Builder
public class TransactionRequest {
private int userId;
private int merchantId;
private int clientReferenceId;
private double amount;
private String createdBy;
}
TransactionResponse
@Component
@Data
@Builder
public class TransactionResponse {
private int userId;
private int merchantId;
private int clientReferenceId;
private double amount;
private LocalDateTime createdAt;
private TransactionStatus status;
}
TransactionDao
@Component
// Dao class
public class TransactionDao {
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());
transactionEntity.setCreatedAt(cuurentTime);
transactionEntity.setUpdatedAt(cuurentTime);
transactionEntity.setExternalId(UUID.randomUUID());
transactionEntity.setStatus(TransactionStatus.CREATED);
transactionRepository.save(transactionEntity);
return TransactionResponse.builder().status(transactionEntity.getStatus())
.createdAt(transactionEntity.getCreatedAt()).build();
}
}
TransactionService
@Service
public class TransactoinService {
@Autowired
public TransactionDao transactionDao;
public TransactionResponse createTransaction(TransactionRequest transactionRequest){
return transactionDao.createTransaction(transactionRequest);
}
}
TransactionController
@RestController
public class TransactionController {
@Autowired
TransactoinService transactoinService;
@PostMapping
TransactionResponse createTransaction(@RequestBody TransactionRequest transactionRequest){
return transactoinService.createTransaction(transactionRequest);
}
}
CodePudding user response:
You should not create your POJO classes as a Spring Bean. Remove @Component
annotation in your TransactionRequest
and TransactionResponse
POJO classes.
CodePudding user response:
The TransactionRequest
is annotated as @Component
so spring boot
autoscan will try to create a @Bean
out that class.
It is also annotated with @Data
so at the time of creating the bean
Spring boot
is trying to inject
other beans as arguments into the all args constructor
, and it is not finding an "int" bean
to inject into the constructor
.
I am guessing that the transaction response should not be a @Component
or at least not a Singleton
bean.