2013-04-24 107 views
0

我们有其他使用spring和cxf的web服务。当抛出一个检查的异常时,我们通过soap ui获得正确的本地化异常。其他Web服务异常返回堆栈跟踪

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

@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

使用Apache-CXF处理JAX-RS webservices中异常流程的最简单方法是通过javax.ws.rs.ext.ExceptionMapper类标记@Provider注释(或者在您的jaxrs:server的Provider部分中) (JAXRS:供应商),你可以在你想返回特定状态代码实例,然后捕获特定的例外:

@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的拦截器框架接管。

+0

但是我们如何处理w ith错误? – 2014-08-30 22:08:19

+0

你是什么意思我们如何处理错误?该框架将通过将其转换为500来处理任何您未明确匹配的内容,这可能是最正确的行为。 – fpmoles 2014-11-13 19:27:09