Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 is have JMeter pick up those JUnits REST requests when I run them. However, to do that, I need to have Spring's RestTemplate send them to the proxy that I'm running JMeter on. So, the question is, how can I do that?

I've done something similar with CXF and their http:conduit and http:client stuff, but I really have no idea how to do this with Spring-MVC.

like image 277
AHungerArtist Avatar asked Sep 10 '10 19:09

AHungerArtist


People also ask

How do I add a proxy to RestTemplate?

Sending a request to a proxy using RestTemplate is pretty simple. All we need to do is to call the setProxy(java. net. Proxy) from SimpleClientHttpRequestFactory before building the RestTemplate object.

What are the different POST method available in RestTemplate to send post request?

Here, we'll try to send POST requests to the Person API by using the POST methods provided by the RestTemplate: postForObject, postForEntity, and postForLocation.

What is RestTemplate Exchange ()?

Rest Template is used to create applications that consume RESTful Web Services. You can use the exchange() method to consume the web services for all HTTP methods. The code given below shows how to create Bean for Rest Template to auto wiring the Rest Template object.

What protocol does RestTemplate use?

That allows the RestTemplate to use the Apache HttpComponents HttpClient under the hood, which definitely supports SSL. It looks like the HttpClient provided by HttpComponentsClientHttpRequestFactory supports SSL out of the box, so there may be almost no configuration required on your side.


2 Answers

@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.

like image 58
CorayThan Avatar answered Oct 12 '22 23:10

CorayThan


Sadly, this was really easy.

 Properties props = System.getProperties(); props.put("http.proxyHost", "localhost"); props.put("http.proxyPort", "9080"); 
like image 42
AHungerArtist Avatar answered Oct 12 '22 23:10

AHungerArtist