【问题标题】:Jax-Ws Custom Validation Error Response MessageJax-Ws 自定义验证错误响应消息
【发布时间】:2016-04-07 03:41:52
【问题描述】:

我想返回我自己的自定义错误消息,在验证请求时需要原始错误消息中的信息。我使用这个@SchemvaValidation 来自定义它。

public class MyErrorHandler extends ValidationErrorHandler {
private final static Logger logger = Logger.getLogger(LoggingHandler.class);

public void warning(SAXParseException exception) throws SAXException {
    logger.warn("warning", exception);
}

public static String message = "";

public void error(SAXParseException exception) throws SAXException {
    logger.error("error", exception);
    message += exception.getMessage();
    throw new SAXParseException("my custom" + exception.getMessage(),null);
}

public void fatalError(SAXParseException exception) throws SAXException {
    logger.fatal("fatal error", exception);
    throw new SAXParseException("my custom" + exception.getMessage(),null);
}

}

问题是如果我抛出我自己的消息,其他错误将被忽略,如果我不这样做,它会在我的 Web 方法之前抛出它自己的异常。你有什么解决办法吗?

【问题讨论】:

    标签: java web-services jaxb jax-ws


    【解决方案1】:

    使用构造函数抛出异常。 SAXParseException(字符串消息,定位器定位器,异常 e)。

    throw new SAXParseException("my custom" + exception.getMessage(),null, exception);
    

    例子:

    try {
        RuntimeException re = new RuntimeException("my runtime exception message");
        SAXException se = new SAXParseException("saxparse 1",null,re);
        SAXException se1 = new SAXParseException("saxparse 2",null,se);
        throw se1;
    
      } catch (SAXException e) {
          Exception e2 = e.getException();
          System.out.println(e2.getMessage());
          System.out.println(e.getMessage());
      }
    

    另外我认为 SAXException 更适合示例中的 SAXParseException。

    【讨论】:

    • 函数错误(SAXParseException异常)被调用了两次,所以如果我第一次抛出,后面会被忽略
    • 我的意思是用更新的调用替换你的 throw 调用。
    • 我需要把它扔到最后一个错误,可能是第 4 个或第 5 个。
    • 了解异常链的概念。每当您抛出异常时,都会在构造函数中传递原始异常。通常所有精心设计的异常都会为构造函数提供这样的选项。因此,即使您在第 4 步或第 5 步中抛出它,也不会丢失原始异常。 docs.oracle.com/javase/tutorial/essential/exceptions/…
    • 问题是我想在最后的时候扔掉,怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 2013-04-08
    • 2011-10-12
    相关资源
    最近更新 更多