soap消息传递和处理(基于Message和Payload的方式)

一、webservice的定义

1. 接口定义:
package com.npf.server;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.npf.model.User;


@WebService
public interface MyService {

	@WebResult(name="addReturnResult")
	public int add(@WebParam(name="a")int a, @WebParam(name="b")int b);
	
	@WebResult(name="loginReturnUser")
	public User login(@WebParam(name="username")String username,@WebParam(name="password")String password);
}
2. 接口实现:
package com.npf.server;

import javax.jws.WebService;

import com.npf.model.User;

@WebService(
endpointInterface="com.npf.server.MyService",
targetNamespace="http://server.npf.com/",
portName="MyServiceImplPort")
public class MyServiceImpl implements MyService {

	@Override
	public int add(int a, int b) {
		System.out.println("a+b"+"="+(a+b));
		return a+b;
	}

	@Override
	public User login(String username, String password) {
		User user = new User();
		user.setId("1");
		user.setUsername("Jack");
		user.setPassword("npf123");
		return user;
	}

}
3. 发布服务:
package com.npf.server;

import javax.xml.ws.Endpoint;

public class ServerStart {
	
	public static void main(String[] args) {
		MyService service = new MyServiceImpl();
		Endpoint.publish("http://localhost:8888/ns", service);
	}

}

二、soap消息传递和处理(基于Message的方式)

package com.npf.client;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;

import org.w3c.dom.Document;


public class Client2 {
	
	public static void main(String[] args) throws Exception {
		//1.创建消息工厂
		MessageFactory messageFactory = MessageFactory.newInstance();
		//2.创建soapMessage
		SOAPMessage soapMessage = messageFactory.createMessage();
		//3.创建soapPart
		SOAPPart soapPart = soapMessage.getSOAPPart();
		//4.获取soapEnvelope
		SOAPEnvelope envelope = soapPart.getEnvelope();
		//5.可以通过soapEnvelope获取有效的header和body信息
		SOAPBody body = envelope.getBody();
		//6.创建一个Qname(Qname就是一个有效的节点)
		QName qname = new QName("http://server.npf.com/","add","tns");
		//7.将节点插入body,作为一个空架的节点
		SOAPBodyElement element = body.addBodyElement(qname);
		//8.在刚才的空架的节点中插入元素
		element.addChildElement("a").setValue("22");
		element.addChildElement("b").setValue("33");
		//soapMessage.writeTo(System.out);
		
		//9.创建服务
		URL uri = new URL("http://localhost:8888/ns?wsdl");
		QName sname = new QName("http://server.npf.com/","MyServiceImplService");
		Service service = Service.create(uri, sname);
		
		//10.创建Dispatch
		Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(
				"http://server.npf.com/","MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE);
		
		SOAPMessage responseSoapMessage = dispatch.invoke(soapMessage);
		responseSoapMessage.writeTo(System.out);
		
		System.out.println("");
		//11.将响应的消息转换成dom对象
		Document document = responseSoapMessage.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
		String content = document.getElementsByTagName("addReturnResult").item(0).getTextContent();
		System.out.println(content);
	}
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值