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

直接上篇,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

如图:
webService之(二)java原生态客户端_第1张图片

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

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

@Test
	public 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消息串

@Test
	public 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);
		
		//创建Dispatch
		Dispatch 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消息串

@Test
	public 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 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 = "" + "";
		System.out.println(payload);
		StreamSource rs = new StreamSource(new StringReader(payload));

		//通过dispatch传递payload
		Source 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
@XmlRootElement
public class Person {
	private String name;
	private String sex;
	private String email;
        //get\set
}



你可能感兴趣的:(WebService)