加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Webservice_15_SOAP异常处理

发布时间:2020-12-17 00:11:46 所属栏目:安全 来源:网络整理
导读:非常感谢孙浩老师。 自定义异常类:UserException package cn.lichen.soap;public class UserException extends Exception {/** * @Fields serialVersionUID : TODO*/ private static final long serialVersionUID = 1L;public UserException() {// TODO Aut

非常感谢孙浩老师。

自定义异常类: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("nn" + "-----------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());
		}
	}
}


客户端异常:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读