[英]Apache CXF JAX-WS return fault without logging exception
I'm trying to return fault SOAP-message with Apache CXF, but only one way I could find it is custom exceptions ( @WebFault
). 我试图用Apache CXF返回错误的SOAP消息,但是我只能找到的一种方法是自定义异常( @WebFault
)。 For example (this is my @WebService
's method): 例如(这是我的@WebService
的方法):
@Override
public String getAuthKey(String username, String password) throws BadCredeintialsException {
UserDetails ud = userDetailsService.loadUserByUsername(username);
String pwd = passwordEncoder.encode(password);
if (pwd.equals(ud.getPassword())) {
return authKeyService.generateAuthKey(ud.getUsername()).getKey();
}
else {
throw new BadCredeintialsException("Wrong username or password", new FaultInfoBean());
}
}
BadCredeintialsException
here is annotated by @WebFault
and extends Exception
(with 2 needed constructors and getFaultInfo()
method). BadCredeintialsException
由@WebFault
注释,并extends Exception
(具有2个所需的构造函数和getFaultInfo()
方法)。
Problem : When exception throws, server prints stack trace of exception into log, but I don't need this, this case (wrong login or pwd) is too low for garbage my server log, I need just return fault as SOAP-message, don't need to throw a real exception. 问题 :当引发异常时,服务器将异常的堆栈跟踪记录打印到日志中,但是我不需要这种情况(这种情况(错误的登录名或密码)对于垃圾服务器日志来说太低了,我只需要返回错误作为SOAP消息,不需要抛出真正的异常。
How can I do this? 我怎样才能做到这一点? Thank you. 谢谢。
I worked around this by defining a FaultHandler Interceptor, and mark exception as known, expected exception. 我通过定义FaultHandler拦截器来解决此问题,并将异常标记为已知的预期异常。 In this case CXF didn't print WARN and exception trace. 在这种情况下,CXF不会打印WARN和异常跟踪。
@Override
public void handleFault(Message message) {
FaultMode mode = message.get(FaultMode.class);
Exception exception = message.getContent(Exception.class);
if (exception != null && exception.getCause() != null) {
if (mode != FaultMode.CHECKED_APPLICATION_FAULT) {
if (exception.getCause() instanceof NotificationFailedException) {
message.put(FaultMode.class, FaultMode.CHECKED_APPLICATION_FAULT);
}
}
}
}
Printed log was below: 打印的日志如下:
INFO Application {http:<url>}NotificationListener#{http://<url>}Notify has thrown exception, unwinding now: NotificationFailedException: Strange Error
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.