【发布时间】:2020-04-22 23:13:39
【问题描述】:
我正在用 java 创建一个 REST API 并在 postman 中对其进行测试,并且在数据库中有纬度和经度,我正在尝试使用 OpenWeather API 根据纬度和经度返回天气数据。但是,在邮递员中对其进行测试时,它返回的是 HTML 而不是我请求的 JSON 数据。
我要测试的路径是
http://localhost:8080/Assignment2C/map/weather/4
我的控制器中的代码是
@GetMapping(value = "weather/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public String getWeather(@PathVariable("id") int id) {
BreweriesGeocode geocode = geocode_service.getGeocode(id);
Breweries brewerie = breweries_service.getBrewerieById(id);
double latitude = geocode.getLatitude();
double longitude = geocode.getLongitude();
String output = "https://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid=4a1f5501b2798f409961c62d384a1c74";
return output;
当使用 Postman 时,它会返回这个
https: //api.openweathermap.org/data/2.5/weather?lat=59.74509811401367&lon=10.213500022888184&appid=4a1f5501b2798f409961c62d384a1c74
但是当我测试邮递员在浏览器中生成的路径时
https://api.openweathermap.org/data/2.5/weather?lat=59.74509811401367&lon=10.213500022888184&appid=4a1f5501b2798f409961c62d384a1c74
它返回正确的 JSON 数据
这是
{"coord":{"lon":10.21,"lat":59.75},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":291.36,"feels_like":289.49,"temp_min":288.71,"temp_max":294.26,"pressure":1028,"humidity":40},"wind":{"speed":0.89,"deg":190},"clouds":{"all":1},"dt":1587551663,"sys":{"type":3,"id":2006615,"country":"NO","sunrise":1587526916,"sunset":1587581574},"timezone":7200,"id":6453372,"name":"Drammen","cod":200}
我在测试时如何让 JSON 数据出现在 postman 中?
【问题讨论】:
标签: java json rest postman openweathermap