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

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

问题描述

我在使用Spring-MVC 3.0实现的开发箱上运行了一个Web服务。我有各种JUnit使用RestTemplate测试该服务。我想要做的是让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:客户端的东西,但我真的有不知道如何使用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,如何首先将请求发送到代理,以便我可以将我的junits与JMeter一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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