调用另一个 Spring Boot 应用中的 REST API。

huangapple go评论124阅读模式
英文:

Call another rest api from own rest api in spring boot application

问题

  1. 我正在学习Spring Boot我已经成功在我的计算机上部署了一个APIAPIOracle获取数据当我在浏览器中粘贴链接http://localhost:8080/myapi/ver1/table1data时,它会返回数据。以下是我的控制器代码:
  2. @CrossOrigin(origins = "http://localhost:8080")
  3. @RestController
  4. @RequestMapping("/myapi/ver1")
  5. public class Table1Controller {
  6. @Autowired
  7. private ITable1Repository table1Repository;
  8. @GetMapping("/table1data")
  9. public List<Table1Entity> getAllTable1Data() {
  10. return table1Repository.findAll();
  11. }
  12. 现在这种情况运行良好我想做另一件事有一个API https://services.odata.org/V3/Northwind/Northwind.svc/Customers 它返回一些客户数据。Spring Boot是否提供了一种方式,让我可以从自己的控制器重新托管/重新部署此API,以便我在浏览器中不是访问上述[链接][1],而是访问http://localhost:8080/myapi/ver1/table1data,然后它会返回相同的客户数据。
  13. [1]: https://services.odata.org/V3/Northwind/Northwind.svc/Customers
英文:

I am learning Spring Boot, I have managed to deploy an API on my computer which fetches data from Oracle and when I paste the link http://localhost:8080/myapi/ver1/table1data in browser it returns me the data. Below is my controller code :

  1. @CrossOrigin(origins = &quot;http://localhost:8080&quot;)
  2. @RestController
  3. @RequestMapping(&quot;/myapi/ver1&quot;)
  4. public class Table1Controller {
  5. @Autowired
  6. private ITable1Repository table1Repository;
  7. @GetMapping(&quot;/table1data&quot;)
  8. public List&lt;Table1Entity&gt; getAllTable1Data() {
  9. return table1Repository.findAll();
  10. }

Now this scenario is working fine. I want to do another thing. There is an API https://services.odata.org/V3/Northwind/Northwind.svc/Customers which returns some customers data. Does spring boot provide any way so that I can re-host/re-deploy this API from my own controller such that instead of hitting this the above link in the browser, I should hit http://localhost:8080/myapi/ver1/table1data and it will return me the same customers data.

答案1

得分: 5

是的,Spring Boot 提供了一种通过 RestTemplate 从应用程序中访问外部 URL 的方式。以下是一个获取响应字符串的示例实现,或者您还可以根据响应使用所需的数据结构进行选择,

  1. @RestController
  2. @RequestMapping("/myapi/ver1")
  3. public class Table1Controller {
  4. @Autowired
  5. private RestTemplate restTemplate;
  6. @GetMapping("/table1data")
  7. public String getFromUrl() throws JsonProcessingException {
  8. return restTemplate.getForObject("https://services.odata.org/V3/Northwind/Northwind.svc/Customers",
  9. String.class);
  10. }
  11. }

您可以创建一个配置类来定义 RestController 的 Bean。以下是代码片段,

  1. @Configuration
  2. public class ApplicationConfig{
  3. @Bean
  4. public RestTemplate restTemplate() {
  5. return new RestTemplate();
  6. }
  7. }
英文:

Yes the spring boot provides a way to hit an external URL from your app via a RestTemplate. Below is a sample implementation of getting the response as string or you can also use a data structure of desired choice depending on the response,

  1. @RestController
  2. @RequestMapping(&quot;/myapi/ver1&quot;)
  3. public class Table1Controller {
  4. @Autowired
  5. private RestTemplate restTemplate;
  6. @GetMapping(&quot;/table1data&quot;)
  7. public String getFromUrl() throws JsonProcessingException {
  8. return restTemplate.getForObject(&quot;https://services.odata.org/V3/Northwind/Northwind.svc/Customers&quot;,
  9. String.class);
  10. }
  11. }

You can create a config class to define the Bean for the rest controller. Below is the snippet,

  1. @Configuration
  2. public class ApplicationConfig{
  3. @Bean
  4. public RestTemplate restTemplate() {
  5. return new RestTemplate();
  6. }
  7. }

答案2

得分: 4

  1. 你可以使用 `RestTemplate` 调用第三方 API 并返回 API 的响应
  2. final String uri = "https://services.odata.org/V3/Northwind/Northwind.svc/Customers";
  3. RestTemplate restTemplate = new RestTemplate();
  4. String result = restTemplate.getForObject(uri, String.class);
  5. [这个网站有一些关于如何使用 Spring RestTemplate 的示例][1]
  6. [1]: https://howtodoinjava.com/spring-boot2/resttemplate/spring-restful-client-resttemplate-example/
英文:

You can use RestTemplate for third party API call and return the response from your API

  1. final String uri = &quot;https://services.odata.org/V3/Northwind/Northwind.svc/Customers&quot;;
  2. RestTemplate restTemplate = new RestTemplate();
  3. String result = restTemplate.getForObject(uri, String.class);

This website has some nice examples for using spring's RestTemplate

答案3

得分: 1

创建一个@Bean的RestTemplate

  1. @Bean
  2. public RestTemplate restTemplate() {
  3. return new RestTemplate();
  4. }

通过使用上述的RestTemplate,你可以从你自己的本地主机获取数据

  1. String url = "https://services.odata.org/V3/Northwind/Northwind.svc/Customers";
  2. restTemplate.getForObject(url, String.class);
英文:

Create a @Bean of RestTemplate

  1. @Bean
  2. public RestTemplate restTemplate() {
  3. return new RestTemplate();
  4. }

By using the above RestTemplate you can fetch the data from ur own localhost

  1. String url = &quot;https://services.odata.org/V3/Northwind/Northwind.svc/Customers&quot;;
  2. restTemplate.getForObject(url,String.class);

huangapple
  • 本文由 发表于 2020年4月5日 16:15:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61039787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定