CXF动态客户端调用webservice实例

private final static String EHR_URL = ConfigUtil.getInstance().getString("ehrUrl");

/**

* 通过卫生局电子健康档案WEBSERVICE接口获取数据

* @param request
* @param retClass
* @return
* @throws JAXBException
*/
public static T getRemoteData(Object request, Class retClass) throws Exception {


JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();


Client client = factory.createClient(EHR_URL);
//initTimeoutConfig(client);


QName name = new QName("http://tempuri.org/", "funMain");

String requestXML = XMLUtil.bean2xml(request);

LOGGER.info("request={}", requestXML);

Object[] objects = client.invoke(name, requestXML);


LOGGER.info("response={}", objects[0]);

return XMLUtil.xml2bean(objects[0].toString(), retClass);


}



private static void initTimeoutConfig(Client client) {
Conduit conduit = client.getConduit();
HTTPConduit hc = (HTTPConduit) conduit;
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setReceiveTimeout(1000 * 10); // 该时间为响应超时。
policy.setConnectionTimeout(1000 * 10); // 连接超时。
hc.setClient(policy);
}



/**
* 将对象转成XML

* @param object
* @return
*/
public static String bean2xml(Object object) {
StringWriter writer = new StringWriter();
JAXBContext context;
try {
context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
marshaller.marshal(object, writer);
} catch (JAXBException e) {
logger.error("对象转XML错误,obj={},err={}", object, e);
}


return writer.getBuffer().toString();
}

你可能感兴趣的:(javaWeb)