我正在尝试使用@WebFault注释,并且我有一个可以抛出几个异常的Web服务,例如下面的示例类MyException1。
根据http://java.globinch.com/enterprise-java/web-services/jax-ws/jax-ws-exceptions-faults-annotation-exception-and-fault-handling-examples/的示例,我编写了每个异常,如MyException1,所有MyException [N]类都相似:它们有一个私有成员,它是Fault POJO / bean和构造函数。
如何避免复制粘贴这样只有类名不同的N类?我不能定义基类并将每个异常子类化,因为它们不能继承构造函数。
@WebFault(name="ServiceFault",targetNamespace="http://somenamespace.com")
public class MyException1 extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
private ServiceFault fault;
public MyException1(String message) {
super(message);
}
public ServiceFault getFault() {
return fault;
}
public MyException1() {
super();
// TODO Auto-generated constructor stub
}
public MyException1(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public MyException1(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public MyException1(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public MyException1(ServiceFault fault) {
super(fault.getFaultString());
this.fault = fault;
}
public MyException1(String message, ServiceFault fault) {
super(message);
this.fault = fault;
}
public MyException1(String message, ServiceFault fault,
Throwable cause) {
super(message, cause);
this.fault = fault;
}
public MyException1(String code, String message) {
super(message);
this.fault = new ServiceFault();
this.fault.setFaultCode(code);
this.fault.setFaultString(message);
}
}
... and similarly for MyException2, ..., MyExceptionN
答案 0 :(得分:1)
选项: -
1)父项MyWSException
有一个 ServiceFault
POJO由子异常类扩展,其名称代表服务中出现的异常。
2)拥有一个通用的MyWSException
类,其详细信息封装在ServiceFault
POJO中。