Rest Web Services Exception返回堆栈跟踪

时间:2013-04-24 17:05:22

标签: rest

我们使用spring和cxf来休息webservices。当抛出一个经过检查的异常时,我们会通过soap ui获得正确的本地化异常。

但是当抛出未经检查的运行时异常时,我们只是得到一个堆栈跟踪,指出无法加载webservice。

@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class WebServiceExceptionWrapper extends Exception {

    /** Type (class name). */
    private String type;

    /** Message. */
    private String message;

    /** Stack trace. */
    private StackTraceElement[] stackTrace;

    /** Cause. */
    private Throwable cause;

    /** The localized message. */
    private String localeErrorCode;

    /**
     * Construct a wrapper around an exception.
     * 
     * @param e exception
     */
    public WebServiceExceptionWrapper(final Exception e) {
        this.type = e.getClass().getName();
        this.message = StringEscapeUtils.escapeHtml(e.getLocalizedMessage());
        this.stackTrace = e.getStackTrace();
        this.cause = e.getCause();
        if(e instanceof DetailedException) {
            this.localeErrorCode = StringEscapeUtils.escapeHtml(((DetailedException) e).getLocaleErrorCode());
        }
    }

    /**
     * Gets the type.
     * 
     * @return the type
     */
    @XmlElement(nillable = true)
    public String getType() {
        return this.type;
    }

    /**
     * Sets the type.
     * 
     * @param type the type to set
     */
    public void setType(final String type) {
        this.type = type;
    }

    /**
     * Gets the message.
     * 
     * @return the message
     */
    @XmlElement(nillable = true)
    public String getMessage() {
        return this.message;
    }

    /**
     * Sets the message.
     * 
     * @param message the message to set
     */
    public void setMessage(final String message) {
        this.message = message;
    }

    /**
     * Gets the stackTrace.
     * 
     * @return the stackTrace
     */
    @XmlElement(nillable = true)
    public StackTraceElement[] getStackTrace() {
        return this.stackTrace;
    }

    /**
     * Sets the stackTrace.
     * 
     * @param stackTrace the stackTrace to set
     */
    public void setStackTrace(final StackTraceElement[] stackTrace) {
        this.stackTrace = stackTrace;
    }

    /**
     * Gets the cause.
     * 
     * @return the cause
     */
    @XmlElement(nillable = true)
    public Throwable getCause() {
        return this.cause;
    }

    /**
     * Sets the cause.
     * 
     * @param cause the cause to set
     */
    public void setCause(final Throwable cause) {
        this.cause = cause;
    }

    /**
     * @return the localeErrorCode
     */
    @XmlElement(nillable = true)
    public String getLocaleErrorCode() {
        return this.localeErrorCode;
    }

    /**
     * @param localeErrorCode the localeErrorCode to set
     */
    public void setLocaleErrorCode(final String localeErrorCode) {
        this.localeErrorCode = localeErrorCode;
    }

我如何防止返回堆栈跟踪。我只想要一个响应代码和一个响应消息。

1 个答案:

答案 0 :(得分:1)

使用Apache-CXF在JAX-RS Web服务中处理异常流的最简单方法是通过标记有@Provider批注的j​​avax.ws.rs.ext.ExceptionMapper类(或者在jaxrs:server的Provider部分中)( jaxrs:providers)。然后,您可以捕获要返回特定状态代码的特定异常,例如:

@Provider
public class FooExceptionMapper implements ExceptionMapper<FooException>  {

    @Override
    public Response toResponse(FooException exception) {
        ... do your work you want to maybe do here
        return Response.status(Response.Status.BAD_REQUEST).entity(...whatever...).build();
    }
}

现在,您为此映射器创建一个bean(或使用注释驱动),将其绑定到您的服务方法,然后捕获您的异常。在你的catch块中,只需抛出它,让cxf的拦截器框架接管。