I'm currently working on a basic angular2/spring website. My goal is to get array of products form my spring backend;
This is how I import the data in my angular component:
this.http.get<Produkt[]>('/get-produkty').subscribe(data => this.produkty = data);
where http is instance of HttpClient.
produkt interface:
interface Produkt { nazwa: string; typ:string; cena:number;}
My spring method:
@RequestMapping(value = "/get-produkty")
public Produkt[] getNames() {
return dao.getProduktyAsTab();
}
produkty is arrayList of type Produkt, I'm trying to convert it to Array. It works fine as long as I convert my list to array like this:
public Produkt[] getProduktyAsTab() {
Produkt[] produktyTest = new Produkt[10];
this.produkty.toArray(produktyTest);
return produktyTest;
}
But it doesn't when I try to do it like this:
public Produkt[] getProduktyAsTab() {
Produkt[] produktyTest = (Produkt[]) this.produkty.toArray();
return produktyTest;
}
I receive response code 500.
HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: "OK", url: "http://localhost:8080/get-produkty", ok: false…}
How do I fix that? Why is this happening?