Witam,
mam następujący problem:
- w controllerze w Springu zwracam posortowaną mapę, której kluczem jest obiekt pożyczki (Loan), a wartością obliczona rata (payment) - poniżej kod:
@RequestMapping(
value = "/calculateLoan/{amount}/{creditPeriod}",
method = {RequestMethod.GET})
public Map<Loan, BigDecimal> allLoanParameters(@PathVariable("amount") int amount,
@PathVariable("creditPeriod") int creditPeriod) {
try {
List<Loan> loans = loanService.findAll();
Map<Loan, BigDecimal> loansWithPayments = loanService.calculateLoanPayment(loans, amount, creditPeriod);
if (loansWithPayments.isEmpty()) {
return null;
}
return loansWithPayments;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
To działa
- Następnie chcę pobrać tę mapę w React - w tym celu mam przygotowany service:
import http from '../http-common';
class HomeDataService {
getAllLoansWithPayments(amount, creditPeriod) {
return http.get(`/calculateLoan/${amount}/${creditPeriod}`);
}
}
export default new HomeDataService();
a w odpowiednim komponencie wygląda to tak:
export default class CalculateLoan extends Component {
constructor(props) {
super(props);
this.getLoans = this.getLoans.bind(this);
this.state = {
loans: []
}
}
}
componentDidMount() {
const amount = this.props.match.params.amount;
const creditPeriod = this.props.match.params.creditPeriod;
this.setState({
amount: amount,
creditPeriod: creditPeriod
});
this.getLoans(amount, creditPeriod);
}
getLoans(amount, creditPeriod) {
HomeDataService.getAllLoansWithPayments(amount, creditPeriod)
.then(response => {
this.setState({
loans: response.data
});
})
.catch(e => {
console.log(e);
});
}
render() {
//
}
}
Ten fragment działa w połowie, tzn. pobieram mapę z ładnie wyliczonymi ratami i posortowaną. Prawie wszystko pasuje, prawie, gdyż zamiast obiektu Loan, pobieram referencję do niego.. I oczywiście przez ten fakt nie mogę pobrać szczegółów obiektu.
Próbowałem na wiele sposobów, ale albo efekt jest gorszy (czyli nie pobieram w ogóle mapy), albo otrzymuję ten sam problem, czyli zamiast obieku, referencję do niego.
Czy może ktoś udzielić wskazówki, w czym rzecz?