使用 RestTemplate,如何首先将请求发送到代理,以便我可以在 JMeter 中使用我的 junit? [英] Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter?

查看:28
本文介绍了使用 RestTemplate,如何首先将请求发送到代理,以便我可以在 JMeter 中使用我的 junit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Spring-MVC 3.0 实现的开发箱上运行了一个 Web 服务.我有各种使用 RestTemplate 测试该服务的 JUnit.我想要做的是让 JMeter 在我运行这些 JUnits REST 请求时接收它们.但是,要做到这一点,我需要让 Spring 的 RestTemplate 将它们发送到我正在运行 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?

我已经用 CXF 和他们的 http:conduit 和 http:client 做了类似的事情,但我真的不知道如何用 Spring-MVC 做到这一点.

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.

推荐答案

@AHungerArtist 的回答适用于简单的用例,您希望所有请求都使用相同的代理.如果您需要通过 restTemplate 的某些请求来使用代理,而其他请求则不需要,您可能会发现这更有用.(或者,如果您只是喜欢以编程方式进行操作,而不喜欢处理系统属性!)

@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);
}

您应该能够以这种方式创建 restTemplate bean 的副本,并以正常方式创建另一个副本,因此您可以使用和不使用代理发送请求.

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.

这篇关于使用 RestTemplate,如何首先将请求发送到代理,以便我可以在 JMeter 中使用我的 junit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆