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

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

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

问题

我正在学习Spring Boot我已经成功在我的计算机上部署了一个API该API从Oracle获取数据当我在浏览器中粘贴链接http://localhost:8080/myapi/ver1/table1data时,它会返回数据。以下是我的控制器代码:

@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping("/myapi/ver1")
public class Table1Controller {

    @Autowired
    private ITable1Repository table1Repository;

    @GetMapping("/table1data")
    public List<Table1Entity> getAllTable1Data() {
        return table1Repository.findAll();
    }

现在这种情况运行良好我想做另一件事有一个API https://services.odata.org/V3/Northwind/Northwind.svc/Customers 它返回一些客户数据。Spring Boot是否提供了一种方式,让我可以从自己的控制器重新托管/重新部署此API,以便我在浏览器中不是访问上述[链接][1],而是访问http://localhost:8080/myapi/ver1/table1data,然后它会返回相同的客户数据。

[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 :

@CrossOrigin(origins = &quot;http://localhost:8080&quot;)
@RestController
@RequestMapping(&quot;/myapi/ver1&quot;)
public class Table1Controller {


    @Autowired
    private ITable1Repository table1Repository;

    @GetMapping(&quot;/table1data&quot;)
    public List&lt;Table1Entity&gt; getAllTable1Data() {
        return table1Repository.findAll();
    }

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 的方式。以下是一个获取响应字符串的示例实现,或者您还可以根据响应使用所需的数据结构进行选择,

@RestController
@RequestMapping("/myapi/ver1")
public class Table1Controller {

   @Autowired
   private RestTemplate restTemplate;

   @GetMapping("/table1data")
   public String getFromUrl() throws JsonProcessingException {
        return restTemplate.getForObject("https://services.odata.org/V3/Northwind/Northwind.svc/Customers",
            String.class);
   }
}

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

@Configuration
public class ApplicationConfig{
	
   @Bean
   public RestTemplate restTemplate() {
       return new RestTemplate();
   }
}
英文:

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,

@RestController
@RequestMapping(&quot;/myapi/ver1&quot;)
public class Table1Controller {

   @Autowired
   private RestTemplate restTemplate;

   @GetMapping(&quot;/table1data&quot;)
   public String getFromUrl() throws JsonProcessingException {
	    return restTemplate.getForObject(&quot;https://services.odata.org/V3/Northwind/Northwind.svc/Customers&quot;,
			String.class);
   }
}

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

@Configuration
public class ApplicationConfig{

   @Bean
   public RestTemplate restTemplate() {
       return new RestTemplate();
   }

}

答案2

得分: 4

你可以使用 `RestTemplate` 调用第三方 API 并返回 API 的响应

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

[这个网站有一些关于如何使用 Spring 的 RestTemplate 的示例][1]

  [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

final String uri = &quot;https://services.odata.org/V3/Northwind/Northwind.svc/Customers&quot;;

RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);

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

答案3

得分: 1

创建一个@Bean的RestTemplate

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

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

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

Create a @Bean of RestTemplate

 @Bean
 public RestTemplate restTemplate() {
     return new RestTemplate();
 }

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

  String url = &quot;https://services.odata.org/V3/Northwind/Northwind.svc/Customers&quot;;
  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:

确定