当前位置: 首页 > 知识库问答 >
问题:

如何使用Spring Boot使用HTTPS GET服务

海叶秋
2023-03-14

我正在尝试使用雅虎气象服务的以下HTTPSendpoint:

雅虎天气服务API

我正在根据API进行一些特殊查询,以获取某些参数化位置的当前天气。

@Service("weatherConditionService")
public class WeatherConditionServiceImpl implements WeatherConditionService {

    private static final String URL = "http://query.yahooapis.com/v1/public/yql";

    public WeatherCondition getCurrentWeatherConditionsFor(Location location) {
        RestTemplate restTemplate = new RestTemplate();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(URL);
        stringBuilder.append("?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22");
        // TODO: Validate YQL query injection
        stringBuilder.append(location.getName());
        stringBuilder.append("%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
        WeatherQuery weatherQuery = restTemplate.getForObject(stringBuilder.toString(), WeatherQuery.class);
        // TODO: Test Json mapping response
        Condition condition = weatherQuery.getQuery().getResults().getChannel().getItem().getCondition();
        return new WeatherCondition(condition.getDate(), Integer.parseInt(condition.getTemp()), condition.getText());
    }

位置是一个提供属性“name”的类,该属性是位置的字符串描述,例如“New York”或“Manila”。

其他类只映射返回的对象。

执行时,我得到以下HTTP响应:

org.springframework.web.client.HttpClientErrorException: 403 Forbidden

这意味着我无权从我所了解的内容访问资源。

如果我只是复制,URL效果很好

雅虎天气查询

我认为映射不是问题,因为我得到的不是“400”(错误请求),而是“403”(禁止)

我使用RestTemplate对象的方式一定有错误。我正在研究,但找不到答案。

共有2个答案

萧玮
2023-03-14

我终于找到了答案。最后,这是一个错误的请求,因为我需要以不同的方式传递参数(而不是作为URL的一部分)。

我在这里找到了答案。这里是我的特定Yahoo Weather API调用返回字符串的代码(我仍然需要做一些工作来使用映射)。

   private static final String URL = "http://query.yahooapis.com/v1/public/yql";

   public String callYahooWeatherApi() {

        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(URL)
                .queryParam("q", "select wind from weather.forecast where woeid=2460286")
                .queryParam("format", "json");

        HttpEntity<?> entity = new HttpEntity<>(headers);

        HttpEntity<String> response = restTemplate.exchange(
                builder.build().encode().toUri(),
                HttpMethod.GET,
                entity,
                String.class);

        return response.getBody();

    }
孙承
2023-03-14

文件说你需要一个api密钥。但当我打这样的电话时:

fetch('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys')
.then(resp=> resp.json())
.then((res)=>console.log(res.query.results))

https://repl.it/NeoM

没有它也能正常工作。也许你因为经常使用api而被黑了。

你的代码看起来不错。

 类似资料:
  • 我已经完成了用户注册和登录。但是我想在创建配置文件时加密密码。 这是我当前的配置 MongoDB连接 模型类:@ Document @ AllArgsConstructor @ NoArgsConstructor @ Data公共类用户{ @Id @Indexed私有字符串Id;@索引私有字符串地址;@索引私有字符串名称;@索引的私有字符串电子邮件;@索引私有字符串密码;@索引私有字符串角色;}

  • 基本上,我正在使用spring boot和thymeleaf创建一个餐厅餐饮网站(只是一个课程的练习),厨师提供自助餐,每个自助餐由不同的菜肴和不同的配料组成(对不起,我的英语)。 我的问题是:我应该使用来指示厨师制作的自助餐吗? 或者我应该直接使用(“/自助餐”)?因为对于配料的控制器,我最终会使用<代码>(“/厨师/{id}/自助餐/{id}/盘子{id}/配料”) ,它看起来很奇怪,但我认为

  • 我使用的是spring boot 1.2.8,但类不存在。没有这门课最好的方法是什么?

  • 因为我有一个长期运行的任务要在我的应用程序的后台执行,所以我正在尝试实现一个服务来完成一项工作。 此服务使用LocationClient实例中的位置数据,每个请求都会触发一个PendingEvent。最低目标版本阻止我使用新的LocationServices,Android文档建议我使用LocationClient的PendingEvent版本。requestLocationUpdates(…)方

  • 我正在开发一个迁移软件,它将使用来自REST服务的未知数据。 我已经考虑过使用MongoDB,但我决定不使用它,而是使用PostgreSQL。 读完本文后,我试图在我的SpringBoot应用程序中使用SpringJPA实现它,但我不知道在我的实体中映射。 我试过了,但什么也不懂! 这里就是我所在的地方: 和... 下表: 我该怎么做? 注意:我不想/不需要一个实体来工作。我的JSON将永远是St

  • 我有Kafka Streams java应用程序启动并运行。我试图使用KSQL创建简单的查询,并使用Kafka流来实现复杂的解决方案。我希望将KSQL和Kafka流作为Java应用程序运行。 我打算通过https://github.com/confluentinc/ksql/blob/master/ksqldb-examples/src/main/java/io/confluent/ksql/em