我有以下课程:
@WebFault(faultBean="com.myapp.FaultBean", name="MyWSException", targetNamespace = "exception.myapp.com")
public class MyWSException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 5911956366562576341L;
private FaultBean faultBean;
public MyWSException(String message, FaultBean faultBean) {
super(message);
this.faultBean = faultBean;
}
public MyWSException(String message, Throwable cause, FaultBean faultBean) {
super(message);
this.faultBean = faultBean;
}
public FaultBean getFaultInfo() {
return faultBean;
}
}
这是我的FaultBean:
@XmlType(name="WSFaultType", namespace = "exception.myapp.com")
@XmlRootElement(name="MyWSException")
@XmlAccessorType(XmlAccessType.FIELD)
public final class FaultBean {
@XmlElement(name="message", required=true)
private String message;
@XmlElement(name="code", required=true)
private String code;
public FaultBean(){
super();
}
public FaultBean( String message, String code) {
super();
this.message= message;
this.code = code;
}
public final String getMessage() {
return message;
}
public final void setMessage(String message) {
this.message= message;
}
public final String getCode() {
return code;
}
public final void setCode(String code) {
this.code = code;
}
}
现在我有两个AOP类,一个用于捕获我的所有自定义异常,另一个用于在提交事务时捕获Spring的异常,以便捕获持久层的所有意外错误:
@Aspect
public class SpringExceptionAspect {
@Pointcut("execution(public * *(..))")
private final void anyPublicOperation() {
}
@Pointcut("execution(void org.springframework.transaction.PlatformTransactionManager.commit(*))")
public final void anyTransactionError() {
}
@AfterThrowing(pointcut = "anyPublicOperation() && anyTransactionError()", throwing = "error")
public void catchException(JoinPoint jp, Throwable error) {
FaultBean bean = new FaultBean(error.getMessage(), "ERRCODE");
throw new MyWSException("Persistence error:", bean);
}
}
@Aspect
public class CustomExceptionAspect{
@Pointcut("execution(public * *(..))")
private final void anyPublicOperation() {
}
@Pointcut("within(com.myapp.service..*)")
public final void anyServerMethod() {
}
@AfterThrowing(pointcut = "anyPublicOperation() && anyServerMethod()", throwing = "error")
public void catchException(JoinPoint jp, Throwable error) {
FaultBean bean = new FaultBean(error.getMessage(), "ERRCODE");
throw new MyWSException("Error:", bean);
}
}
现在当Exceptions被第二个Aspect(当我抛出自定义异常时)捕获时,一切正常,我得到了预期的soap:fault响应。但是当Spring捕获了自己的Transaction异常时,我得到了这个回复:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring
Marshalling Error: neither class com.myapp.FaultBean nor any of its superclasses are known to this conext</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
如果他们是同一个班级,怎么会发生这种情况?为什么Jax-b能够从一个点而不是从另一个点来编组那些?