Using RestClient, i don't know how implement proxy, need to declare the port and host

84 Views Asked by At

I want to implement proxy in host and port, i tryed increment in to url but give errors i used spring.cloud.bootstrap.proxy.host=proxy1.lan.bnm.md spring.cloud.bootstrap.proxy.port=8080 but its not work, i use spring-boot-starter-web 3.2.2 in RestTemplate i know that use in that case:


        String proxyHost = "proxy.zxc.asd";
        int proxyPort = 8080;

        // Создание объекта RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // Создание объекта прокси
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
        requestFactory.setProxy(proxy);
        restTemplate.setRequestFactory(requestFactory);

        // URL для отправки запроса
        String url = "https://jsonplaceholder.typicode.com/todos";

        // Отправка GET-запроса и получение ответа
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

        // Вывод ответа
        System.out.println("Response: " + response.getBody());

I have this class and do not know how implement proxy.

package org.example;

import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

import java.util.List;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Bean
    RestClient restClient(RestClient.Builder builder) {
        return builder.baseUrl("https://jsonplaceholder.typicode.com/todos").build();
    }

    @Bean
    TodoClient todoClient(RestClient restClient) {
        HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(
                RestClientAdapter.create(restClient)).build();
        return factory.createClient(TodoClient.class);
    }

    @Bean
    ApplicationRunner applicationRunner(TodoClient todoClient) {
        return args -> {
            var response = todoClient.todos();
            System.out.println(response);
        };
    }

    record Todo(Long id, long userId, String title) {}

    interface TodoClient {
        @GetExchange
        List<Todo> todos();
    }
}

I tried write in to application.properties, use quarkus, create Web Config. I read Spring Docs, but not find https://docs.spring.io/spring-framework/reference/integration/rest-clients.html

just this -

RestClient restClient = RestClient.builder().baseUrl("https://api.github.com/").build();
RestClientAdapter adapter = RestClientAdapter.create(restClient);
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();

RepositoryService service = factory.createClient(RepositoryService.class);

I think that code is right but i have problem with proxy, in this case i get Connection timed out: connect

0

There are 0 best solutions below