webservice客户端使用jdk自带wsimport实现方案

来源:互联网 发布:sql declare 编辑:程序博客网 时间:2024/05/21 15:52

webservice客户端步骤

  1. 根据xml使用trang.jar生成xsd文件
  2. 根据xsd文件,使用trang生成model
  3. 根据wsdl地址,使用jdk自带的wsimport工具生成客户端工具代码
  4. 使用工具连接service服务器。

wsimport生成客户端工具代码命令:

wsimport -s . -p com.simbest.cert.webservice.apply http://124.207.215.118:18576/openapi/services/PersonCertApply?wsdl

-p:生成的类的包路径

-s:生成的代码所在路径

根据xml生成xsd文件命令例子:

java -jar trang.jar -I xml -O xsd F:\mail.xml F:\mail.xsd

根据xsd生成bean,xjc是jdk自带工具

xjc -encoding UTF-8 -d . -p com.simbest.maip.si.plan.model.next ./tasknext.xsd

-p:生成的类的包路径

-d:生成的xsd文件所在路径

生成的工具使用例子:

String str="";Test_Service ps=new Test_Service();//Test_service和TestPortType都是生成的工具类TestPortType port = ps.getTestHttpPort();str = port.personTest(requestXML);//发送请求并获取返回值

beanToXml:

 public static String beanToXml(Object obj, Class<?> load) throws JAXBException {        JAXBContext context = JAXBContext.newInstance(load);        Marshaller marshaller = context.createMarshaller();        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");        StringWriter writer = new StringWriter();        marshaller.marshal(obj, writer);        return writer.toString();    }

xmlToBean:

 public static Object xmlToBean(String xml, Class<?> load) throws JAXBException, IOException {        JAXBContext context = JAXBContext.newInstance(load);        Unmarshaller unmarshaller = context.createUnmarshaller();        Object object = unmarshaller.unmarshal(new StringReader(xml));        return object;    }
原创粉丝点击