我有一个控制器,它使用RestTemplate向外部API发送请求并获取数据 struct .它应该返回这个 struct ,但在使用我的控制器时,有时会丢弃JSON响应的结尾.
我try 定制RestTemplate并增加应用程序中的响应大小.yaml
我的控制器:
@GetMapping(value = "/weather/lat={lat}&lon={lon}")
@Cacheable(value = "root", key = "{#lat , #lon}")
public ResponseEntity<Root> getWeather(@PathVariable("lat") String lat, @PathVariable("lon") String lon) {
try{
String request = String.format("%s?lat=%s&lon=%s&units=metric&appid=%s",
weatherUrl, lat, lon, appId);
return restTemplate.getForEntity(request, Root.class);
}catch (RestClientException e){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
截断查询示例:
{
"coord": {
"lat": 1.4,
"lon": 2.33
},
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04n"
}
],
"base": "stations",
"main": {
"temp": 26.15,
"feels_like": 26.15,
"temp_min": 26.15,
"temp_max": 26.15,
"pressure": 1014,
"humidity": 79,
"sea_level": 1014,
"grnd_level": 1014
},
"visibility": 10000,
"wind": {
"speed": 6.34,
"deg": 198,
"gust": 6.08
},
"clouds": {
"all": 100
},
"dt": 1696111416,
"sys": {
"sunrise": 1696052261,
"sunset": 1696095829
},
"timezone": 0,
"id": 0,
"name":"co
应用程序.yaml:
spring:
application:
name: weather-service
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
boot:
admin:
client:
instance:
api-gateway-port: 17957
management-base-url: http://localhost:${spring.boot.admin.client.instance.api-gateway-port}
eureka:
instance:
instance-id: ${spring.application.name}:${random.value}
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
appid: *********************
url:
external-api:
weather: https://api.openweathermap.org/data/2.5/weather
server:
port: 0