webService之(二)java原生态客户端

来源:互联网 发布:邮箱正则表达式java 编辑:程序博客网 时间:2024/05/21 09:29
直接上篇,java生成webService客户,
jdk自带命令wsimport,jdk/bin目录下有wsimport.exe文件,在doc命令行下,输入
wsimport -keep -p com.xyj.client http://localhost:8889/ms?wsdl
命令参数说明:
  -d:生成客户端执行类的class文件的存放目录
  -s:生成客户端执行类的源文件的存放目录
  -p:定义生成类的包名
  其他命令参数请参照:http://download-llnw.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html

如图:


将上述生成的文件拷贝到项目中,即可使用。

客户端调用有两种方式:
一、利用生成的客户端代码

@Testpublic void test001() throws Exception{QName qname = new QName("http://web.xyj.com/", "PersonServiceService");URL url = new URL("http://localhost:8889/ms?wsdl");IPersonService service = new PersonServiceService().getPersonServicePort();Person person = service.getPersonInfos();System.out.println("name:" + person.getName() + ",sex:" + person.getSex() + ",email:" + person.getEmail());}

二、发soap消息

1、java api生成soap消息串

@Testpublic void test002() throws Exception {URL url = new URL("http://localhost:8889/ms?wsdl");QName qn = new QName("http://web.xyj.com/", "PersonServiceService");Service service = Service.create(url, qn);//创建DispatchDispatch<SOAPMessage> dispatch = service.createDispatch(new QName("http://web.xyj.com/", "PersonServicePort"),SOAPMessage.class, Service.Mode.MESSAGE);//拼装请求消息串SOAPMessage message = MessageFactory.newInstance().createMessage();SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();SOAPBody body = envelope.getBody();QName eName = new QName("http://web.xyj.com/", "getPersonInfos", "ns");SOAPBodyElement ele = body.addBodyElement(eName);//拼装参数//ele.addChildElement("count").setValue("9");message.writeTo(System.out);System.out.println("-------------");SOAPMessage response = dispatch.invoke(message);response.writeTo(System.out);System.out.println();Document doc =  response.getSOAPBody().extractContentAsDocument();NodeList nl = doc.getElementsByTagName("person");JAXBContext ctx = JAXBContext.newInstance(com.xyj.test.Person.class);for (int i = 0; i < nl.getLength(); i++) {Node n = nl.item(i);com.xyj.test.Person person = (com.xyj.test.Person) ctx.createUnmarshaller().unmarshal(n);System.out.println("name:" + person.getName() + ",sex:" + person.getSex() + ",email:" + person.getEmail());}System.out.println("\n================");for (int i = 0; i < nl.getLength(); i++) {Element elem = (Element) nl.item(i);Node name = elem.getElementsByTagName("name").item(0);Node sex = elem.getElementsByTagName("sex").item(0);Node email = elem.getElementsByTagName("email").item(0);System.out.println("name:" + name.getTextContent() + ",sex:" + sex.getTextContent() + ",email:" + email.getTextContent());} }

2、直接拼接soap消息串

@Testpublic void test003() throws Exception{URL url = new URL("http://localhost:8889/ms?wsdl");QName sname = new QName("http://web.xyj.com/", "PersonServiceService");Service service = Service.create(url, sname);Dispatch<Source> dispatch = service.createDispatch(new QName("http://web.xyj.com/","PersonServicePort"), Source.class, Service.Mode.PAYLOAD);System.out.println("=====================");/** * 封装相应的part getPersonInfos,如有参数,也想应装载 * 可以通过Marshaller列化回 XML数据 * com.xyj.test.Person person = new com.xyj.test.Person(); * person.setName("123"); *  * JAXBContext ctx = JAXBContext.newInstance(com.xyj.test.Person.class); * Marshaller mar = ctx.createMarshaller(); * mar.setProperty(Marshaller.JAXB_FRAGMENT, true); * StringWriter writer = new StringWriter(); * mar.marshal(person, writer); *  */String payload = "<ns:getPersonInfos xmlns:ns=\"" + "http://web.xyj.com/" + "\">" + "</ns:getPersonInfos>";System.out.println(payload);StreamSource rs = new StreamSource(new StringReader(payload));//通过dispatch传递payloadSource response = (Source) dispatch.invoke(rs);//将Source转化为DOM进行操作,使用Transform对象转换Transformer tran = TransformerFactory.newInstance().newTransformer();DOMResult result = new DOMResult();tran.transform(response, result);//处理相应信息(通过xpath处理)XPath xpath = XPathFactory.newInstance().newXPath();NodeList nl = (NodeList) xpath.evaluate("//person", result.getNode(), XPathConstants.NODESET);JAXBContext ctx = JAXBContext.newInstance(com.xyj.test.Person.class);com.xyj.test.Person person = (com.xyj.test.Person) ctx.createUnmarshaller().unmarshal(nl.item(0));System.out.println("name:" + person.getName() + ",sex:" + person.getSex() + ",email:" + person.getEmail());}

Person.java

//一定要标为XmlRootElement@XmlRootElementpublic class Person {private String name;private String sex;private String email;        //get\set}



0 0
原创粉丝点击