Я получил ответ массива из следующего кода.
Я могу вернуть результат массива, как указано выше, но как мне вернуть объект json, используя одно из значений из этого массива? Я очень новичок в Java, Springboot и Hibernate. Любая помощь будет оценена!
GoalPlanController
@RequestMapping(method = RequestMethod.GET, path="/calculateRecurringAmount")
public ResponseEntity<Object> getCalculateRecurringAmount( String accountID) {
try {
logger.info("get recurring amount by accountid:->", accountID);
AccountsDTO[] goalPlan = goalPlanService.getCalculateRecurringAmount(accountID);
return new ResponseEntity<>(goalPlan, HttpStatus.OK);
}catch(Exception ex) {
logger.error("Exception raised retriving recurring amount using accountId:->" + ex);
ErrorDTO errors = new ErrorDTO();
errors.setError(ex.getMessage());
errors.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());
return new ResponseEntity<>(errors, HttpStatus.SERVICE_UNAVAILABLE);
}
}
Это GoalPlanDaoImplementation
@Autowired
private GoalPlanRepository goalPlanRepository;
@Override
public List<Accounts> getCalculateRecurringAmount(String accountID) {
// TODO Auto-generated method stub
List<Accounts> goalPlan = null;
goalPlan = goalPlanRepository.getCalculateRecurringAmount(accountID);
return goalPlan.subList(0, 1);
}
GoalPlanRepository ->
public interface GoalPlanRepository extends JpaRepository<GoalPlan, String>{
@Query("select ac from Accounts ac where ac.userId = :accountID")
public List<Accounts> getCalculateRecurringAmount(@Param("accountID") String accountID);
}
Я получаю результат массива следующим образом
{
"accountID": "acc12345",
"accountName": "hellooee",
"accountType": "goalPlanner",
"userId": "abcd",
"bankName": null,
"bankId": null,
"debiitCardNumber": null,
"availableBalance": null,
}
]```
Now using accountID I need to return a json object like this
{
"calculatedGoalAmount": [
{
"goalFrequency": "Monthly",
"goalAmount": 0.4166666666666667,
"fromAccount": "acc12345"
},
{
"goalFrequency": "Quarterly",
"goalAmount": 1.25,
"fromAccount": "acc12345"
}
]
}
My AccountsDTO has folllowing
public class AccountsDTO {
private String accountID;
private String accountName;
private String accountType;
private String userId;
private String bankName;
private String bankId;
private String debitCardNumber;
//getters and setters
}
And initilAmount, goalTimePeriod, goalAmount are the values entered by user.
then i need to calculate
monthly = (goalAmount-initialAmount)/(12*goalTimePeriod)
quarterly = (goalAmount-initialAmount)/(4*goalTimePeriod)
accountId = (got from the response array above)