I'm doing some hands-on on Springboot with Microservices and REST. Have created 3 Microservices : 1. Account 2. FundTransfer 3. APIGateway
Account Service is running on port 5051, FundTransfer service is running on 5052. While API Gateway is running on default port 8080.
Request is successfully being hit to both Account and FundTransfer service through their respective ports and able to do both GET and POST ops. Also, DB interaction is working fine.
Problem is : While trying to pass the request through API Gateway, POST operation on both Services is working finr and I'm able to store request data in DB. But can't see the success response in Postman. Instead, getting 500 Internal Server Error despite successfull storing of data in DB.
Same goes for GET requests on both Account and FundTransfer services via API Gateway. Same 500 Internal Server Error issue.
Wondering what could cause this. Don't see any code issue here. Someone help resolve this guys.
Here are Controller, Service and configuration codes.
Account Controller :
@RestController
@RequestMapping("/banking/account")
public class AccountController {
@Autowired AccountService accService;
@PostMapping("/createAccount")
public Account createAccount(@RequestBody Account acc) {
return accService.createAccount(acc);
}
@GetMapping("/getBankAccounts")
public List<Account> getBankAccounts() {
return accService.getBankAccounts();
}
@GetMapping("/getSpecificAccount/{accountNumber}")
public Account getOneAccount(@PathVariable String accountNumber) {
return accService.getSingleAccount(accountNumber);
}
}
*Account Service Impl : *
@Service
public class AccountServiceImpl implements AccountService{
@Autowired AccountRepo accountRepository;
@Override
public Account createAccount(Account account) {
return accountRepository.save(account);
}
@Override
public List<Account> getBankAccounts() {
return accountRepository.findAll();
}
@Override
public Account getSingleAccount(String accountNumber) {
return accountRepository.findById(accountNumber).get(); //To avoid getting nullPointerException since we've not put NotNull validation for accNum and so it may have null value as well.
}
}
FundTransfer Controller :
@RestController
@RequestMapping("/banking/fundTransfer")
public class FundTransferController {
@Autowired
FundTransferService fundTransfer;
@PostMapping("/newFundTransferRequest")
public FundTransfer processTransaction(@RequestBody FundTransfer fundDetails) {
return fundTransfer.processTransaction(fundDetails);
}
@GetMapping("/getTransactions")
public List<FundTransfer> getTransactions(){
return fundTransfer.getTransactions();
}
@GetMapping("/getTxnForAccount/{accountNumber}")
public List<FundTransfer> getTransactionsForAccount(String accountNumber) {
return fundTransfer.getTransactionsForAccount(accountNumber);
}
}
FundTransfer Service Impl :
@Service
public class FundTransferServiceImpl implements FundTransferService{
@Autowired
FundTransferRepo fundTransferRepository;
@Override
public FundTransfer processTransaction(FundTransfer fundTransfer) {
return fundTransferRepository.save(fundTransfer);
}
@Override
public List<FundTransfer> getTransactions() {
return fundTransferRepository.findAll();
}
@Override
public List<FundTransfer> getTransactionsForAccount(String accountNumber) {
return fundTransferRepository.findByAccountFrom(accountNumber);
}
}
API Gateway yaml file :
spring:
application:
name: banking-gateway
cloud:
gateway:
mvc:
routes:
- id: account_route
uri:
http://localhost:5051
predicates:
- Path=/banking/account/**
- id: fund_transfer
uri:
http://localhost:5052
predicates:
- Header=Service, Fund-Transfer
Account Service application.properties :
server.port=5051
spring.application.name= AccountService
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:AccountService_DB
Fund Transfer Service application.properties :
server.port=5052
spring.application.name=FundTransferService
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:FundTransfer_DB
Postman Error : Postman Error Screenshot