Java / Angular: keyvalue pipe doesn’t show data sent from backend

huangapple go评论91阅读模式
英文:

Java / Angular: keyvalue pipe doesn't show data sent from backend

问题

以下为您翻译好的内容:

我试图建立一个应用程序,其中后端从API接收数据并将其传递到前端。该API的网址是https://www.travel-advisory.info/api

长话短说:由于某种原因,我似乎在前端接收到了数据,但是我无法在表格或列表中显示它。

作为第一步,我想获取所有数据,因为最终我想使其持久化,所以我已经在使用带有GET方法的Spring。我在Java中的控制器:

(Java控制器代码)

我的Java模型:

(Java模型代码)

嗯...然后我使用相同的模式也有了“Advisory.java”、“Api_status.java”、“Reply.java”、“Request.java”等对象。在我的Angular项目中,我同样在.ts文件中拥有相同的模型。此外,我有这个服务:

(Angular服务代码)

...以及这个组件:

(Angular组件代码)

在HTML中:

(HTML代码)

有趣的是,当我运行Spring Boot和ng serve时,json管道完美地显示了来自API的所有数据。
然而,表格中没有数据。然而,最令我困惑的是,在本地主机上的浏览器中显示了一个具有正确行数但没有内容的表格。

我在浏览器的开发工具或bash中都没有收到任何错误消息。

谢谢您的帮助,如果您需要任何额外的信息,我将很乐意提供!

英文:

I'am trying to set up an app in which the backend receives data from an api and proceeds it to the frontend. The api is https://www.travel-advisory.info/api

Long story short: For some reason, I seem to receive the data in the frontend, but I cannot display it in a table or list.

As a first step, I want to get all data, and as I eventually want to make it persistent I am already using Spring with the get method. My controller in Java:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.savetravel.SaveTravel.TravelWarning.TravelWarningObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;


import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/gettravelwarnings")
public class TravelWarningController {
	
	@GetMapping("/alltw")
	public TravelWarningObject getAllTravelWarnings()
			throws JsonMappingException, JsonProcessingException {
		
		RestTemplate restTemplate = new RestTemplate();
		TravelWarningObject allTravelWarnings = new TravelWarningObject();
		
		ResponseEntity<TravelWarningObject> responseEntity = restTemplate.getForEntity(
				"https://www.travel-advisory.info/api", TravelWarningObject.class);
		allTravelWarnings = responseEntity.getBody();
	
		return allTravelWarnings;
				
	}
}

My Model in Java:

package com.savetravel.SaveTravel.TravelWarning;

import java.util.HashMap;

public class TravelWarningObject {

	private Api_status api_status;
	private HashMap<String, CountryCode> data;

public TravelWarningObject () {
	
}

public HashMap<String, CountryCode> getData() {
	return data;
}

public void setData(HashMap<String, CountryCode> data) {
	this.data = data;
}

public Api_status getApi_status() {
	return api_status;
}

public void setApi_status(Api_status api_status) {
	this.api_status = api_status;
}

}

and

package com.savetravel.SaveTravel.TravelWarning;


public class CountryCode {

	private String iso_alpha_2;
	private String name;
	private String continent; 
	private Advisory advisory;
	
	public CountryCode() {
		
	}
	
	public CountryCode(String iso_alpha_2, String name, String continent,
			com.savetravel.SaveTravel.TravelWarning.Advisory advisory) {
		super();
		this.iso_alpha_2 = iso_alpha_2;
		this.name = name;
		this.continent = continent;
		this.advisory = advisory;
	}

	public String getIso_alpha_2() {
		return iso_alpha_2;
	}

	public void setIso_alpha_2(String iso_alpha_2) {
		this.iso_alpha_2 =  iso_alpha_2;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContinent() {
		return continent;
	}

	public void setContinent(String continent) {
		this.continent = continent;
	}

	public Advisory getAdvisory() {
		return advisory;
	}

	public void setAdvisory(Advisory advisory) {
		this.advisory = advisory;
	}
			
}

Well...and using the same pattern I have also the objects "Advisory.java", "Api_status.java", "Reply.java","Request.java". In my Angular project, I have, analogously, the same Models in .ts . Additionally, I have this service:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { TravelWarningObject } from '../Models/TravelWarningObject';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class CountryDataServiceService {

  constructor(private http: HttpClient) { }

rootApiPath:string = "http://localhost:8080/gettravelwarnings"


public getAllTWs(): Observable<TravelWarningObject> {
return this.http.get<TravelWarningObject>(this.rootApiPath + "/alltw");

}
}

..and this component:

import { Component, OnInit } from '@angular/core';
import { CountryDataServiceService } from '../service/country-data-service.service';
import { TravelWarningObject } from '../Models/TravelWarningObject';
import { CountryCode } from '../Models/CountryCode';

@Component({
  selector: 'app-all-travel-warnings',
  templateUrl: './all-travel-warnings.component.html',
  styleUrls: ['./all-travel-warnings.component.css']
})
export class AllTravelWarningsComponent implements OnInit {


travelWarningObject = new TravelWarningObject;

  constructor(private countryDataService: CountryDataServiceService) { }

  ngOnInit(): void {

    this.countryDataService.getAllTWs().subscribe(fetchedTWO => (this.travelWarningObject = fetchedTWO));
           
  }

}

and in html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

<table border="1">

<thead>
    <tr>
<th>Country</th>
<th>Code</th>
</tr>
</thead>
    <tbody>
    <tr *ngFor = "let value of travelWarningObject.data | keyvalue">
        <td>{{value.name}}</td>
        <td>{{value.iso_alpha2}}</td>
    </tr>
</tbody>
</table>


{{travelWarningObject | json }}
<br />
--------
<br />
{{travelWarningObject.data | json }}
<br />

</body>
</html>

The funny thing now is that the json pipe shows perfectly all the data from the api when I run Spring Boot and ng serve.
However, there is no data in the table. Still, and thats the most bewildering thing to me, a table is shown in the browser on localhost:4200 which has the right number of rows but no content.

I don't receie any error message whatsoever, neither in the dev tools of my browser nor in the bash.

Thanks for your help, if you need any kind of additional information I'd be happy to provide it!

Linux version 4.8.0-53-generic (buildd@lgw01-56) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) ) #56~16.04.1-Ubuntu SMP Tue May 16 01:18:56 UTC 2017
------
openjdk version "13.0.1-BellSoft" 2019-10-15
------
Angular CLI: 9.0.6
Node: 10.22.0

答案1

得分: 0

按名称提示Keyvalue,它返回keyvalue。像下面这样使用它来获取你的结果。

<tr *ngFor="let item of travelWarningObject.data | keyvalue">
    <td>{{item.key}}</td>
    <td>{{item.value.iso_alpha2}}</td>
</tr>
英文:

As name suggests Keyvalue,it returns key and value. Use it like below get your result.

&lt;tr *ngFor = &quot;let item of travelWarningObject.data | keyvalue&quot;&gt;
    &lt;td&gt;{{item.key}}&lt;/td&gt;
    &lt;td&gt;{{item.value.iso_alpha2}}&lt;/td&gt;
&lt;/tr&gt;

huangapple
  • 本文由 发表于 2020年9月16日 21:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63920789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定