第一种原生的调用方式:
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
Map<String, Object> outProps = new HashMap<String, Object>();
outProps.put(WSHandlerConstants.ACTION,WSHandlerConstants.USERNAME_TOKEN);
outProps.put(WSHandlerConstants.USER, "admin");
outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,MyClientPasswordCallback.class.getName());
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
factory.getOutInterceptors().add(wssOut);
//
factory.setServiceClass(YourClass.class);
factory.setAddress("youraddress?wsdl");
YourService client = (YourService) factory.create();
try {
String ss = client.yourMethod("parameters");
System.out.println(ss);
} catch(Exception e){
e.printStackTrace();
}
第二种 与spring 配合调用
1.在配置文件中配置:
<bean id="clientCallback" class="com.travelsky.adap.re.ras.da.service.test.MyClientPasswordCallback">
<property name="userIdentity">
<map>
<entry key="test" value="test" />
</map>
</property>
</bean>
<bean id="wss4jOut" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordText" />
<entry key="user" value="test" />
<entry key="passwordCallbackRef"> <ref bean="clientCallback" />
</entry>
</map>
</constructor-arg>
</bean>
<jaxws:client id="client"
serviceClass="yourclass" address="youraddress">
<jaxws:outInterceptors>
<ref bean="wss4jOut" />
</jaxws:outInterceptors>
</jaxws:client>
2.单元测试
private ApplicationContext context;
@Before
public void init()
{
this.context = new ClassPathXmlApplicationContext("classpath*:applicationContext-cxf-client.xml");
}
@Test
public void test() throws InterruptedException, RasException
{
YourService client=(YourService )context.getBean("client");
long start = System.currentTimeMillis();
/*设置查询参数*/
System.out.println("request string:"+req);
//调用方法
String a = client.clsChangeAnalyzeWs(req);
System.out.println("the return datas:"+a);
long end = System.currentTimeMillis();
System.out.println((end-start)/1000.0);
}