在SOAP Web服务中的一个地方处理运行时异常,并抛出带有自定义错误的Fault

时间:2018-11-19 11:27:57

标签: spring-boot soap exception-handling cxf soapfault

我有SOAP服务(spring boot + CXF),该服务会引发故障。现在我的代码看起来像这样

@Component
@Slf4j
public class MyEndpoint implements MessageExchangePortType {

    private final Void aVoid = new Void();

    @Override
    public SendRequestResponse sendRequest(SendRequestRequest request) throws MessageIsAlreadySentException{
        try {
            myHandler.request(...);
         } catch (MessageIdAlreadyExistsException e) {
            log.error(e.getMessage(), e);
            throw new MessageIsAlreadySentException(FaultString.MESSAGE_IS_ALREADY_SENT.getMessage(), aVoid);
        }
    }

这是我的例外:

public class MessageIdAlreadyExistsException extends Exception {}

我从类的最深处一直将该异常转发到顶部,以捕获并抛出另一个异常,该异常位于web方法的签名中:

} catch (MessageIdAlreadyExistsException e) {//my exception
                throw new MessageIsAlreadySentException(FaultString.MESSAGE_IS_ALREADY_SENT.getMessage(), aVoid); //web service exception
            }

这是网络服务例外:

@WebFault(name = "MessageIsAlreadySent", targetNamespace = "")
public class MessageIsAlreadySentException
    extends Exception
{

嘴上的表情是Checked,但我想要某种类似Spring REST的机制。我想从出现此异常的地方抛出RuntimeException,然后在一个地方处理它,设置消息并返回给客户。像这样:

  @ExceptionHandler(EmailSendException.class)
  protected ResponseEntity<RestResponse<Boolean>> handleEmailSendException(EmailSendException e) {
    ...
  }

如何使用SOPA Web服务?

0 个答案:

没有答案