Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter?

后端 未结 5 1778
夕颜
夕颜 2020-12-02 22:06

I have a web service running on my dev box implemented using Spring-MVC 3.0. I have various JUnits that test against that service using RestTemplate. What I would like to do

5条回答
  •  无人及你
    2020-12-02 22:57

    @AHungerArtist's answer works for simple use cases, where you want all requests to use the same proxy. If you need some requests through restTemplate to use the proxy, and others to not, though, you may find this more useful. (Or if you just like doing it programmatically more than you like mucking with system properties!)

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    
        Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("my.host.com", 8080));
        requestFactory.setProxy(proxy);
    
        return new RestTemplate(requestFactory);
    }
    

    You should be able to create a copy of the restTemplate bean that way, and another one the normal way, so you can send requests with and without the proxy.

提交回复
热议问题