简体   繁体   English

如何为客户端和服务器配置Spring Boot RestTemplate代理

[英]How to configure spring boot resttemplate proxy for client and server

I have an architecture where my server component will be deployed on separate host and client component (UI) will be deployed on separate. 我有一个体系结构,其中我的服务器组件将部署在单独的主机上,而客户端组件(UI)将部署在单独的主机上。

I am stuck with RestTemplate Proxy, can someone please help me how can I achieve it. 我一直在使用RestTemplate代理,有人可以帮助我如何实现它。

Below is the example, I am trying to follow, but not sure if its the right approach. 下面是示例,我尝试遵循,但不确定其正确方法。

@Value("${generic.proxyHost}")
private String proxyHost;

@Value("${generic.proxyPort}")
private Integer proxyPort;

@Bean
public RestTemplate restTemplate() {

    LOGGER.info("Setting up proxy with HOSTNAME => " + proxyHost + " and PORT => " + proxyPort);

    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();

    Proxy proxy= new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
    requestFactory.setProxy(proxy);

    return new RestTemplate(requestFactory);
}

Also it would be help if I can know how to handle multipart file request. 如果我知道如何处理多部分文件请求,那也将有所帮助。

Any help will be greatly appericiated. 任何帮助都将得到极大的重视。

I need to consume REST API on separate host and I am just looking for an example. 我需要在单独的主机上使用REST API,我只是在寻找示例。 I just googled around the stuff but no luck 我只是用谷歌搜索东西,但是没有运气

There's a nice tutorial about Rest Template at Baeldung's blog . Baeldung的博客上有一个关于Rest Template的很好的教程。

You could use this simple example to understand how to use it: 您可以使用以下简单示例来了解如何使用它:

RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl = "http://localhost:8080/spring-rest/foos";
ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));

There's a plenty of examples in the article I linked above that will help you during your learn path. 我上面链接的文章中有很多示例,它们将在您的学习道路上为您提供帮助。

Also it would be help if I can know how to handle multipart file request. 如果我知道如何处理多部分文件请求,那也将有所帮助。

I believe that this other question has the information you need to start implementing this use case. 我相信这另一个问题提供了开始实施此用例所需的信息。

Cheers! 干杯!

暂无
暂无

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

相关问题 如何为 Spring 中的每个客户端配置和构建自定义 RestTemplate? - How to configure and build a custom RestTemplate for each client in Spring? 如何像 Spring Boot 方式一样为 Micronaut (1.1.4) HTTP 客户端配置 HTTP 代理? - How can I configure the HTTP proxy for a Micronaut (1.1.4) HTTP client like the Spring Boot way? 如何在 spring 引导中为嵌入式 tomcat 服务器在 apache2 中配置反向代理? - How to configure reverse proxy in apache2 for embedded tomcat server in spring boot? 测试Spring Boot RestTemplate客户端的JSON映射 - Testing JSON mapping for a Spring Boot RestTemplate client Spring Cloud-如何在Zuul反向代理上配置OAuth2RestTemplate来传递授权令牌? - Spring Cloud - How to configure OAuth2RestTemplate on Zuul reverse proxy to pass authorization tokens through? 使用spring-cloud-config-client时如何配置自定义RestTemplate? - How to configure a custom RestTemplate when using spring-cloud-config-client? 当 Spring Boot 中的响应正文无效时,如何使用 RestTemplate 客户端进行 GET 请求? - How to use RestTemplate client for GET request when response body is void in Spring Boot? 如何在 Spring Boot 中使用 RestTemplate 发送表情符号? - How to Send Emojis using RestTemplate in Spring Boot? 如何在 Java Spring boot 中模拟 RestTemplate? - How to mock RestTemplate in Java Spring boot? 如何在Spring Boot中为RestTemplate编写JUnit测试 - How to write a JUnit Test in Spring Boot for RestTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM