使用CXF中的javax.ws.rs.client.ClientBuilder创建客户端,是否可以使用本地传输的任何路由? [英] Using javax.ws.rs.client.ClientBuilder in CXF to create Client, any route to be able to use local transport?
问题描述
我正在使用从CXF发行版使用标准"javax.ws.rs.client.ClientBuilder
"类的代码库来配置和创建"javax.ws.rs.client.Client
".
I work on a codebase that uses the standard "javax.ws.rs.client.ClientBuilder
" class, from the CXF distribution, to configure and create a "javax.ws.rs.client.Client
".
这很好用.
我现在正在尝试编写测试,这些测试使用内联类定义的控制器使用JAXRSServerFactoryBean
来管理假服务器.在测试和客户端配置中,我都可以将host:port设置为localhost:something,并且效果很好,足以让我测试MessageBodyReaders和Http异常处理.
I'm now trying to write tests that use JAXRSServerFactoryBean
to manage a fake server using a controller defined by an inline class. I can set my host:port to localhost:something, both in the test and in the client configuration, and this works well enough to allow me to test our MessageBodyReaders and Http exception handling.
但是,我认为这不是可扩展的",因为每个假服务器都必须在专用"端口上运行(至少在运行测试时).我可以尝试使用不常见的端口,并让不同的测试使用不同的端口,或者使用随机数,但这都有些冒险.我真的不希望CI构建失败,因为并行运行的测试最终使用了相同的端口.
However, I think this won't be "scalable", as each fake server will have to run on a "dedicated" port (while running the test, at least). I can try to use uncommon ports, and have different tests use different ports, or use random numbers, but that's all somewhat risky. I don't really want CI builds to fail because tests running in parallel ended up using the same port.
I read about the ability in CXF (not JAX-RS) to use "local transport" (https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Testing). It appears that might resolve my problem. I need to verify this, but it's possible that two tests running in parallel both using local transport will not conflict.
但是,我什至无法使它工作,因为我们的客户端代码使用的是标准" JAX-RS客户端类,而不是CXF客户端类.它们似乎不同且不兼容.
However, I can't even get this to work yet, because our client code is using the "standard" JAX-RS client class, not the CXF one. They appear to be different and incompatible.
在创建客户端时,我尝试执行此操作(只是看它是否可以工作):
At the point where I create the client, I tried to do this (just to see if it can work):
WebClient.getConfig(client).getRequestContext().put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE);
不幸的是,此操作失败,并在"org.apache.cxf.jaxrs.client.WebClient.getConfig(Object)
"中显示不是有效的客户端",因为它必须是"org.apache.cxf.jaxrs.client.Client
"的实例,而不是javax.ws.rs.client.Client
.
Unfortunately, this fails with "Not a valid Client" in "org.apache.cxf.jaxrs.client.WebClient.getConfig(Object)
" because it needs to be an instance of "org.apache.cxf.jaxrs.client.Client
", not javax.ws.rs.client.Client
.
这里是否有任何简单(甚至可能)的路径?
Is there any easy (or even possible) path forward here?
推荐答案
You can use ClientRequestFilters to unit test JAX-RS clients. Basically register a custom ClientRequestFilter to your Client object (or ClientBuilder) that mocks your response using the abortWith(Response)
method on the ClientRequestContext
object that is passed in to the filter method.
类似的事情应该起作用:
Something like this should work:
public MyMockRequestFilter implements ClientRequestFilter {
@Override
public void filter(ClientRequestContext requestContext) {
MyEntity entity = // get the entity you want to mock as returned from the server
requestContext.abortWith(Request.ok(entity).build());
}
}
...
ClientBuilder builder = ClientBuilder.newBuilder().register(MyMockRequestFilter .class)
希望这会有所帮助, 安迪
Hope this helps, Andy
这篇关于使用CXF中的javax.ws.rs.client.ClientBuilder创建客户端,是否可以使用本地传输的任何路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!