有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用Rest模板创建包含变量的URL?

我不知道如何创建URL。我想通过表单从用户那里获得城市的价值。AppID和app。我从应用程序获取的myserviceWeatherUrl。yml。如何连接URL以获得类似以下内容:app。myserviceWeatherUrl?q=城市和城市;应用程序。阿皮德

@Service
public class WeatherClient {

@Value("{app.APPID}")
private String appID;
@Value("{app.myserviceWeatherUrl}")
private String baseUrl;

private final RestTemplate restTemplate;


public WeatherClient(RestTemplate restTemplate) {
    this.restTemplate = restTemplate;
}

public WeatherDto getWeather(String city) {
    try {
        return restTemplate.getForObject(baseUrl + city + appID, WeatherDto.class);
    } catch (Exception e) {
        throw new DataNotAvailableException();
    }
  }
}

共 (1) 个答案

  1. # 1 楼答案

    使用巧妙命名为URL的Java类