微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Webservice_15_SOAP异常处理

非常感谢孙浩老师。

自定义异常类:UserException

package cn.lichen.soap;

public class UserException extends Exception {

	/** 
	* @Fields serialVersionUID : Todo
	*/ 
	private static final long serialVersionUID = 1L;

	public UserException() {
		// 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
	}

	public UserException(String message,Throwable cause) {
		super(message,cause);
		// 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
	}

}


接口:IMyService

@WebResult(name = "user")
	public User login(@WebParam(name = "username") String username,@WebParam(name = "password") String password) throws UserException;

 

实现:MyServiceImpl

@Override
	public User login(String username,String password) throws UserException {
		for(User user:users) {
			if(username.equals(user.getUsername())&&password.equals(user.getpassword()))
				return user;
		}
		throw new UserException("用户不存在"); 


 

测试方法

/**
	 * @Title: test07
	 * @Description: SOAP处理异常
	 * @param
	 * @return void
	 * @throws
	 */
	@Test
	public void test07() {
		try {
			// 创建访问wsdl服务的URL
			URL url = new URL("http://localhost:9999/ns?wsdl");
			// 通过Qname指明服务的具体信息
			QName name = new QName("http://soap.lichen.cn/","MyServiceImplService");
			// 创建service
			Service service = Service.create(url,name);

			// 创建dispatch
			dispatch<SOAPMessage> dispatch = service.createdispatch(new QName(
					"http://soap.lichen.cn/","MyServiceImplPort"),SOAPMessage.class,Service.Mode.MESSAGE);

			// 创建SOAPmessage
			SOAPMessage message = MessageFactory.newInstance().createMessage();
			SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
			SOAPBody body = envelope.getBody();
			
			QName qname = new QName("http://soap.lichen.cn/","login","xsd");
			SOAPBodyElement bodyElement = body.addBodyElement(qname);
			bodyElement.addChildElement("username").setValue("aaa");
			bodyElement.addChildElement("password").setValue("bbb");
			// 输入创建SOAPmessage
			message.writeto(System.out);

			System.out.println("\n\n" + "-----------invoking-------------"
					+ "\n");

			// 传递消息并且得到结果
			SOAPMessage responseMessage = dispatch.invoke(message);

			// 输出得到的SOAPmessage
			responseMessage.writeto(System.out);

		} catch (SOAPException e) {
			e.printstacktrace();
		} catch (IOException e) {
			e.printstacktrace();
		}
	}


客户端控制台报异常,显示消息:


服务端控制台报没有异常:

修改实现方法扑获的异常:

@Override
	public User login(String username,String password) throws UserException {
		for(User user:users) {
			if(username.equals(user.getUsername())&&password.equals(user.getpassword()))
				return user;
		}
		throw new RuntimeException("用户不存在"); 
	}


 

由于wsd中的声明的异常类型为UserException,

 

如果为UserException,服务器发现此类型,所以服务器不会报异常,但会返回客户端异常。

当报异常RuntimeException时,服务器没有发现此类型,所以服务器报异常,同时会返回客户端异常。

 

使用wsimport导出服务端Java文件,创建客户端。会产生UserException_Exception.java。

测试方法:TestClient

package cn.lichen.soap;

public class TestClient {

	public static void main(String[] args) {
		MyServiceImplService mis = new MyServiceImplService();
		IMyService ms = mis.getMyServiceImplPort();
		try {
			ms.login("admin","34545");
		} catch (UserException_Exception e) {
			System.out.println(e.getMessage());
		}
	}
}


客户端异常:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐