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

无法使用resttemplate使用rest api

束俊英
2023-03-14

我正在尝试使用这个api,这是链接https://www.live-rates.com/api/price?key=123456&rate=EUR_USD

我已经在我的控制器中尝试过了

@ResponseBody
    @RequestMapping(value = { "/start" }, method = RequestMethod.GET)
    public String start() { 
RestTemplate restTemplate = new RestTemplate();

        String quote = restTemplate.getForObject("https://www.live-rates.com/api/price?key=123456&rate=EUR_USD", String.class);
        return quote.toString();
    }

在我的日程安排程序中

package com.boilerplate.components;


import java.util.Date;
import org.slf4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Async;

import com.boilerplate.services.MessageListenerService;
import java.util.Random;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

@Repository
@Transactional
@Configuration
@EnableAsync
@EnableScheduling
public class Scheduler {
//Database read and update and delete
    @Autowired
    private RabbitTemplate rabbitTemplate;

    private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(MessageListenerService.class);

    @Autowired 
    private SessionFactory sessionFactory;

    @Async
    @Scheduled(cron="*/60 * * * * *")
    public void doSomething(){

        RestTemplate restTemplate = new RestTemplate();

        String quote = restTemplate.getForObject("https://www.live-rates.com/api/price?key=123456&rate=EUR_USD", String.class);
        LOGGER.info(quote.toString());

  }


    }

而他们都没能拿到汇率。

Server Error

为什么我不能使用rest模板获得汇率。

共有1个答案

颜瀚漠
2023-03-14

您是否尝试添加下面的代码。您正在尝试使用HTTPS调用。

CloseBleHttpClient httpClient=HttpClients.Custom().SetSSLHostNameVerifier(new NoPhostNameVerifier()).build();HttpComponentsClientHttpRequestFactory requestFactory=新的HttpComponentsClientHttpRequestFactory();RequestFactory.SetHttpClient(httpClient);RestTemplate RestTemplate=新建RestTemplate(requestFactoryString quote=restTemplate.getForObject(“https://www.live-rates.com/api/price?key=123456&rate=EUR_USD”,String.class);

 类似资料:
  • 我正在发送API和接收状态代码400与我需要解析的正文 在使用RestTemplate时,我无法解析响应: 在这种情况下,我怎么还能得到正文/标题?我必须使用RestTemplate的替代方案吗? 此外,当我在内遇到捕获异常时,如何返回到请求上下文:

  • 我试图调用一个Spring Cloud Data Flow REST Endpoint,它应该返回一个任务的所有执行列表,该任务的名称在输入中传递。 首先,我在浏览器中运行了以下网址: 浏览器上显示了以下JSON: 接下来,我尝试通过Java调用同一个RESTendpoint;然而,无论我尝试什么,响应对象似乎都是空的,没有填充任何属性: 方法1:创建自定义域类来反序列化响应。(不工作。响应中收到

  • 我需要一个用于私有方法测试的powermock 如果我只使用@RunWith(PowerMockRunner.class)而不使用@powermockrunnerregate(SpringJUnit4ClassRunner.class),效果会很好 我需要使用@powermockrunnerregate(SpringJUnit4ClassRunner.class)来表示@autowired。但它并

  • 我试图使用来生成。出于某种原因,我无法重现我使用创建的,该代码可以毫无问题地运行。 下面是我的调用成功并返回200:

  • 我不能用HttpEntity调用Spring RestTem板进行POST请求。我调用RestTem板给出Base64字符串在邮递员,但使用我的java实现,它会给出以下错误: 我的实施是: 我尝试过以下解决方案,但在这里不起作用 这个我也提到过这个

  • 我正在尝试评估使用Consul进行服务发现,但在一个简单的项目中,RestTemboard不是自动连线的。使用Spring Cloud Consul 1.0.0。M6和Spring Boot 1.3.3。 查看Consul UI,我可以看到我的“多媒体”服务已注册,如果我在下面的代码中注释了RestTemplate的使用,我可以查看服务实例信息。 目前1.0.0.M6的Consul discove