简体   繁体   English

REQ:Spring配置的CXF ws客户端-替换“地址”之类的硬编码变量

[英]REQ: Spring configured CXF ws client — replacing hardcoded variables like 'address'

I have to contact a Web Service (WS). 我必须联系Web服务(WS)。 I'm using JDK 1.6, maven 3.04, Spring 3.20 and apache's CXF 2.7.2. 我正在使用JDK 1.6,maven 3.04,Spring 3.20和apache的CXF 2.7.2。 Using maven I created stubs for the WS by feeding it the wsdl file. 使用Maven,我通过向wsdl文件提供WS为WS创建了存根。 I have a spring config for the WS client and it looks something like 我有一个用于WS客户端的spring配置,它看起来像

servlet-context.xml servlet的context.xml中

<jaxws:client id="paymentClient" serviceClass="com.xxx.payment.Payment"
    address="http://127.0.0:8088/mockPaymentBinding" <!-- SOAPUI mock -->
    username="username"
    password="secret" />
<!- username and password are for wsdl basic authentication -->

In the Java code it looks something like 在Java代码中,它看起来像

@Autowired 
com.xxx.payment.Payment client;
..
// Set all needed parameters.
PaymentGetBalanceResponse response = null;
PaymentGetBalance getBalance = new PaymentGetBalance();
RequestGetBalance value = new RequestGetBalance();
value.setTransactionId("transActionId");
getBalance.setRequest(value );

// Now call the WS and get the response
response = client.getBalance(getBalance); // generated by the cxf -client argument.

The "response" line is generated as an example by CXF. 作为示例,CXF生成了“响应”行。 Then Eclipse tells me something is missing (getbalance) and optionally creates it for me above the line. 然后Eclipse告诉我缺少了什么(getbalance),并有选择地为我在行上方创建它。 Then something else is (value) missing and so on. 然后,其他东西(价值)缺失了,依此类推。 In the end all parameters are correctly filled in. All the missing stuff/variables/objects are in the generated stubs code. 最后,所有参数均正确填写。所有缺少的填充/变量/对象都在生成的存根代码中。

This works like a charm BUT the address is atm hardcoded in the spring config. 这就像一个魅力,但地址在spring配置中是atm硬编码的。 The configuarion parameters for the application are stored in a simple database. 该应用程序的配置参数存储在一个简单的数据库中。 The contents is accesible using a spring bean so I can get at the variables in the end in the code using something like config.getValue(URL); 内容可以使用spring bean访问,因此我可以使用config.getValue(URL)之类的代码来获取变量的结尾。

I hoped to being able to change the 'address' (url WS) in the code above but haven't found a way to do that. 我希望能够在上面的代码中更改“地址”(URL WS),但还没有找到一种方法。 Can't find setters in the generated stub code. 在生成的存根代码中找不到设置器。 An alternative would be to use variables in the spring servlet-context.xml file BUT those variables have to come from the database. 一种替代方法是在spring servlet-context.xml文件中使用变量,但这些变量必须来自数据库。 Second alternative. 第二种选择。 I probably/hopefully get away with starting at the bottom and using the Objectfactorys (in the stubs) to create objects. 我可能/希望从底部开始并使用Objectfactorys(在存根中)创建对象。 Then setting the correct parameter (either in the 'new' or a setter) and then work my way to the top. 然后设置正确的参数(在“ new”或setter中),然后按我的方式操作到顶部。 A colleguee has done this (not for 'address') and this seems to work but the code is suboptimal/'messy' at best. 一位同事已经做到了这一点(不是为了“地址”),这似乎可行,但是代码充其量只是次优/“混乱”。 ALSO I would like to able to have the username and password configurable, NOT static. 我还希望能够配置用户名和密码,而不是静态的。 Did quite a bit of RTM at the CXF sites but to no avail. 在CXF站点进行了大量RTM,但无济于事。

Read something about JaxWsProxyFactoryBean but can't figure out how to apply it here as I use Springs @autowire functionality. 阅读有关JaxWsProxyFactoryBean的内容,但是由于我使用Springs @autowire功能,因此无法在此处弄清楚如何应用它。

I've been breaking my brains about this issue but it seems my neurons are running in circles. 我一直在为这个问题而绞尽脑汁,但似乎我的神经元正在运转。 ANY help/pointers is really appreciated. 真的很感谢任何帮助/指针。

From CXF User Guide: How to override the service address? 来自CXF用户指南: 如何覆盖服务地址? .

If I've inferred the spring config correctly, I think this will do: 如果我正确推断了spring配置,我认为这可以做到:

   @Autowired
   com.xxx.payment.Payment client;
   // ... 
   BindingProvider provider = (BindingProvider)client.getServicePort();
   // You can set the address per request here
   provider.getRequestContext().put(
        BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
        "http://my/new/url/to/the/service");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM