soap中自定义异常处理

创建服务器端

1.创建一个接口

IMyService.java

package com.test.service;

import java.util.List;

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

@WebService(targetNamespace = "http://www.webservice.com")
public interface IMyService {
	// 异常处理
	@WebResult(name = "user")
	public User login(@WebParam(name = "username") String username, @WebParam(name = "password") String password) throws UserException;
}

 

2.创建实现接口的类

MyServiceImpl.java

package com.test.service;

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

import javax.jws.WebService;

@WebService(endpointInterface = "com.test.service.IMyService", targetNamespace = "http://www.webservice.com")
public class MyServiceImpl implements IMyService {

	private static List<User> users = new ArrayList<User>();

	public MyServiceImpl() {
		users.add(new User(1, "admin", "管理员", "111111"));
	}

	@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("用户名或密码不正确!");
	}
}

 

3.自定义异常类

UserException.java

package com.test.service;

public class UserException extends Exception {

	public UserException() {
		super();
	}

	public UserException(String message, Throwable cause) {
		super(message, cause);
	}

	public UserException(String message) {
		super(message);
	}

	public UserException(Throwable cause) {
		super(cause);
	}

}

 

4.开启服务

MyServer.java

package com.test.service;

import javax.xml.ws.Endpoint;

public class MyServer {
	public static void main(String[] args) {
		String address = "http://localhost:9999/server";
		// 发布服务的实现类
		Endpoint.publish(address, new MyServiceImpl());
	}
}

 

5.测试类TestSoap.java

// 异常处理
@Test
public void test05() {
	try {
		// 创建服务
		URL url = new URL("http://localhost:9999/server");
		QName qname = new QName("http://www.webservice.com", "MyServiceImplService");
		Service service = Service.create(url, qname);

		// 创建Dispatch
		Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName("http://www.webservice.com", "MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE);

		// 创建SOAP的body消息
		SOAPMessage message = MessageFactory.newInstance().createMessage();
		SOAPBody body = message.getSOAPPart().getEnvelope().getBody();

		// 根据QName创建相应的节点
		QName ename = new QName("http://www.webservice.com", "login", "ns");
		SOAPBodyElement element = body.addBodyElement(ename);
		element.addChildElement("username").setValue("cdxs");
		element.addChildElement("password").setValue("123");
		message.writeTo(System.out);
		System.out.println("\n invoking...");

		// 通过dispatch传递消息,返回响应消息
		SOAPMessage response = dispatch.invoke(message);
		response.writeTo(System.out);
		System.out.println();
	} catch (SOAPException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} catch (SOAPFaultException e) {
		System.out.println(e.getMessage());
	}
}

 

运行结果:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns:login xmlns:ns="http://www.webservice.com"><username>cdxs</username><password>123</password></ns:login></SOAP-ENV:Body></SOAP-ENV:Envelope>

 invoking...

用户名或密码不正确!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 SOAP添加自定义信息,你需要使用 SOAP 头处理程序(SOAP Header Handler)来实现。在 C++ ,可以通过以下步骤来实现: 1. 创建一个 SOAP 头处理程序类,继承自 gsoapsoap_header 类。 2. 在类添加要添加到 SOAP自定义信息字段。 3. 实现 soap_header 类的 virtual 函数,用于在 SOAP 消息添加自定义信息。 4. 在 gsoap 生成的客户端或服务端代码,使用 soap_register_plugin() 函数注册 SOAP 头处理程序。 下面是一个简单的示例代码,用于在 SOAP添加自定义信息: ```c++ #include "soapH.h" class MySoapHeader : public soap_header { public: int myCustomField; MySoapHeader() : myCustomField(0) {} virtual int put(struct soap *soap, const char *tag, int id, void *ptr, size_t len) { if (strcmp(tag, "MyCustomField") == 0) { soap_element(soap, "MyCustomField", &myCustomField, sizeof(myCustomField), XSD_INT); return SOAP_OK; } return soap_header::put(soap, tag, id, ptr, len); } }; int main() { struct soap soap; soap_init(&soap); // 创建 SOAP 头处理程序 MySoapHeader header; header.myCustomField = 123; // 注册 SOAP 头处理程序 soap_register_plugin(&soap, &header); // 发送 SOAP 消息 // ... soap_destroy(&soap); soap_end(&soap); soap_done(&soap); return 0; } ``` 在上面的代码,MySoapHeader 类继承自 soap_header 类,添加了一个名为 myCustomField 的自定义字段。在 put() 函数,如果 SOAP 消息的标签为 "MyCustomField",则将 MyCustomField 字段添加到 SOAP 消息。最后,在 main() 函数,创建了一个 MySoapHeader 实例并注册到 soap 对象

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值