简体   繁体   English

从 API 回答的 JSON 内容返回 null(使用 Spring 引导)

[英]JSON contents answered from an API returns null (with Spring boot)

I'm learning to consume API with Spring boot and faced the following problem:我正在学习使用 Spring Boot 使用 API 并遇到以下问题:

The URL "http://servicebus2.caixa.gov.br/portaldeloterias/api/home/ultimos-resultados" returns a JSON with the last results from Brazil's Lottery. URL“http://servicebus2.caixa.gov.br/portaldeloterias/api/home/ultimos-resultados”返回一个 JSON,其中包含巴西彩票的最后结果。

When trying to receive this output with a Spring controller:尝试使用 Spring 控制器接收此输出时:

@GetMapping(value = "/loterias")
public String getLotteryResults() {
    String url = "http://servicebus2.caixa.gov.br/portaldeloterias/api/home/ultimos-resultados";
    RestTemplate rest = new RestTemplate();
    Object results = rest.getForObject(url, Object.class);

    return results.toString();

I receive the following error message:我收到以下错误消息:

2022-07-10 12:22:14.692[0;39m [31mERROR[0;39m [35m17456[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.accC[.[.[/].[dispatcherServlet] [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 2022-07-10 12:22:14.692[0;39m [31mERROR[0;39m [35m17456[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.accC[.[.[/].[dispatcherServlet] [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] 在路径 [] 的上下文中抛出异常 [请求处理失败; nested exception is java.lang.NullPointerException: Cannot invoke "Object.toString()" because "results" is null] with root cause嵌套异常是 java.lang.NullPointerException:无法调用“Object.toString()”,因为“results”为空] 根本原因

I'm following the tutorial from "https://www.geeksforgeeks.org/how-to-call-or-consume-external-api-in-spring-boot/" (it works using the new link "https://restcountries.com/v3.1/all").我正在关注“https://www.geeksforgeeks.org/how-to-call-or-consume-external-api-in-spring-boot/”中的教程(它使用新链接“https:// /restcountries.com/v3.1/all”)。

I had already tried the same output class as in the example with no success... Any hints on what can be done in this case?我已经尝试过与示例中相同的输出类,但没有成功......关于在这种情况下可以做什么的任何提示?

It seems that the link you posted is not active anymore "http://servicebus2.caixa.gov.br/portaldeloterias/api/home/ultimos-resultados"您发布的链接似乎不再有效"http://servicebus2.caixa.gov.br/portaldeloterias/api/home/ultimos-resultados"

on chrome it sends a redirection to it's https endpoint:在 chrome 上它发送一个重定向到它的 https 端点: 在此处输入图像描述

When tried using restTemplate.getForEntity(url, Object.class) it shows a 302-redirect, modern browsers will automatically redirect hence it's transparent for the user.当尝试使用restTemplate.getForEntity(url, Object.class)时,它会显示 302 重定向,现代浏览器会自动重定向,因此它对用户是透明的。

<302,[Location:"https://servicebus2.caixa.gov.br/portaldeloterias/api/home/ultimos-resultados", Connection:"close"]> <302,[位置:“https://servicebus2.caixa.gov.br/portaldeloterias/api/home/ultimos-resultados”,连接:“关闭”]>

Try to directly use its https endpoint, but this also means you need to handle the security certs guide尝试直接使用其 https 端点,但这也意味着您需要处理安全证书指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM