WebService-向服务提供方传递SOAP信息

TestSoap:

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.bind.JAXBContext;
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.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;

public class TestSoap {

	//创建消息Message
	public static SOAPMessage test1() throws Exception {
		// 创建消息工厂
		MessageFactory factory=MessageFactory.newInstance();
		//根据消息工厂创建SoapMessage
		SOAPMessage SOAPmessage=factory.createMessage();
		//创建SOAPPart
		SOAPPart SOAPpart=SOAPmessage.getSOAPPart();
		//获取SOAPEnvelope
		SOAPEnvelope SOAPenvelope=SOAPpart.getEnvelope();
		//通过soapEnvelope有效的获取相应的Body和Header的信息
			//处理SOAPHeader信息
		SOAPHeader SOAPheader=SOAPenvelope.getHeader();
		if(SOAPheader==null)SOAPheader=SOAPenvelope.addHeader();
		QName hname=new QName("http://www.mytargrtNamespace.com", "authInfo", "nn");
		SOAPHeaderElement SOAPheaderElement =SOAPheader.addHeaderElement(hname);
		SOAPheaderElement.setValue("aabbccddasd");
			//处理SOAPBody信息
		SOAPBody SOAPbody=SOAPenvelope.getBody();
		//根据Qname创建相应的节点(QName就是一个带有命名空间的节点)
		String namespaceURI="http://www.mytargrtNamespace.com";
		String localPart="login";
		String prefix="ns";//前缀,可随意定义
		QName qname=new QName(namespaceURI, localPart, prefix);//
//		SOAPbody.addBodyElement(qname).setValue("123");
		
		SOAPBodyElement ele=SOAPbody.addBodyElement(qname);
		ele.addChildElement("username").setValue("admin");
		ele.addChildElement("password").setValue("111111");
		//打印消息信息
//		SOAPmessage.writeTo(System.out);
		return SOAPmessage;
	}

	public static void main(String []args) throws Exception{
		//创建服务
		URL wsdlDocumentLocation=new URL("http://localhost:6666/ms?wsdl");
		
		QName serviceName=new QName("http://service.soap.org/", "MyServiceImplService");
		
		Service service=Service.create(wsdlDocumentLocation, serviceName);
		//创建Dispatch
		QName portName=new QName("http://service.soap.org/","MyServiceImplPort");
		Class type=SOAPMessage.class;
		Mode mode=Service.Mode.MESSAGE;
		Dispatch dispatch=service.createDispatch(portName, type, mode);
	
		//创建SOAPMessage
		SOAPMessage msg=test1();
		
		//通过Dispath传递消息
		SOAPMessage response=dispatch.invoke(msg);
		response.writeTo(System.out);
	}
}
====================================================================================================================================

服务提供方服务:

MyServer:

package org.soap.service;

import javax.xml.ws.Endpoint;

public class MyServer {
	public static void main(String []args){
		Endpoint.publish("http://localhost:6666/ms", new MyServiceImpl());
	}
}
IMyService:
package org.soap.service;

import java.util.List;

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

@WebService(targetNamespace="http://www.mytargrtNamespace.com")
public interface IMyService {
	@WebResult(name="addResult")
	public int add(@WebParam(name="a")int a,@WebParam(name="b")int b);
	
	@WebResult(name="user")
	public User addUser(@WebParam(name="user")User user);
	
	@WebResult(name="user")
	public User login(@WebParam(name="username")String username,@WebParam(name="password")String password) throws UserException;
	
	@WebResult(name="user")
	public List list(@WebParam(header=true,name="authInfo")String authInfo);
}
MyServiceImpl:
package org.soap.service;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;
import javax.xml.ws.WebServiceClient;

@WebService(endpointInterface="org.soap.service.IMyService")
public class MyServiceImpl implements IMyService{

	private static List users=new ArrayList();
	
	public MyServiceImpl(){
		User user=new User();
		user.setId(1);
		user.setNickname("管理员");
		user.setPassword("111111");
		user.setUsername("admin");
		users.add(user);
	}
	
	public int add(int a, int b) {
		System.out.println(a+"+"+b+"="+(a+b));
		return a+b;
	}

	@Override
	public User addUser(User user) {
		// TODO Auto-generated method stub
		users.add(user);
		return user;
	}

	@Override
	public User login(String username, String password) throws UserException{
		// TODO Auto-generated method stub
		for(User user:users){
			if(user.getUsername().equals(username)&&user.getPassword().equals(password)){
				return user;
			}
		}
//		return null;
		throw new UserException("用户不存在");
	}

	@Override
	public List list(String authInfo) {
		// TODO Auto-generated method stub
		System.out.println(authInfo);
		return users;
	}

}
UserException:
package org.soap.service;

public class UserException extends Exception {

	public UserException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		// TODO Auto-generated constructor stub
	}

	public UserException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public UserException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	public UserException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}

}


你可能感兴趣的:(WebService)