2016-01-28 53 views
3

我對Jersey服務使用Jersey和Spring。爲了捕獲異常並格式化發送給調用者的響應,我添加了ExceptionMapper的實現。澤西島自定義ExceptionMapper不會在400錯誤請求上調用(當驗證失敗時)

雖然在我明確地從控制器中拋出一個異常時調用它,但是當json字段驗證失敗時,異常映射器不會被調用,並且發送的響應是**可能不爲null(path = checkNotification.arg0 .tyres,一個InvalidValue = NULL) **

@Provider 
public class GenericExceptionMapper implements ExceptionMapper<Throwable> { 

    @Override 
    public Response toResponse(Throwable ex) { 
     System.out.println("Exception Mapper !!!"); 
     return Response.status(404).entity(ex.getMessage()).type("application/json").build(); 
    } 
} 


<servlet> 
     <servlet-name>jersey-servlet</servlet-name> 
     <servlet-class> 
      org.glassfish.jersey.servlet.ServletContainer 
     </servlet-class> 
      <init-param> 
       <param-name>jersey.config.server.provider.packages</param-name> 
       <param-value> 
         com.help.rest.controller, 
         com.fasterxml.jackson.jaxrs.json 
       </param-value> 
      </init-param> 
      <init-param> 
        <param-name>javax.ws.rs.Application</param-name> 
        <param-value>com.help.filter.FeatureRegistration</param-value> 
      </init-param> 
      <init-param> 
       <param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name> 
       <param-value>true</param-value> 
      </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

球衣版本是2.22.1 Spring版本是4.2.4

我把它通過改變

@Provider 
    public class GenericExceptionMapper implements ExceptionMapper<Throwable> { 

     @Override 
     public Response toResponse(Throwable ex) { 
      System.out.println("Exception Mapper !!!"); 
      return Response.status(404).entity(ex.getMessage()).type("application/json").build(); 
     } 
    } 
工作

@Provider 
    public class GenericExceptionMapper implements ExceptionMapper<ConstraintViolationException> { 

     @Override 
     public Response toResponse(ConstraintViolationException ex) { 
      System.out.println("Exception Mapper !!!"); 
      return Response.status(404).entity(ex.getMessage()).type("application/json").build(); 
     } 
    } 

雖然我能趕上,我沒有得到它無法約束的確切類和現場異常。


解決 找到所有包含在ContraintViolationException必填字段一套,可以用ex.getConstraintViolations()

+0

你註冊過你的異常映射器嗎? – ACV

+1

用於bean驗證的映射器處理異常。只能使用一個映射器。我想這是你想要的,因爲你啓用了錯誤實體輸出。如果沒有,您可以添加您自己的ConstraintViolationException的映射器來覆蓋澤西島使用的映射器 –

+0

是的,我已經註冊了Exception Mapper類。 –

回答

2

使它通過改變工作

@Provider 
    public class GenericExceptionMapper implements ExceptionMapper<Throwable> { 

     @Override 
     public Response toResponse(Throwable ex) { 
      System.out.println("Exception Mapper !!!"); 
      return Response.status(404).entity(ex.getMessage()).type("application/json").build(); 
     } 
    } 

@Provider 
    public class GenericExceptionMapper implements ExceptionMapper<ConstraintViolationException> { 

     @Override 
     public Response toResponse(ConstraintViolationException ex) { 
      System.out.println("Exception Mapper !!!"); 
      return Response.status(404).entity(ex.getMessage()).type("application/json").build(); 
     } 
    } 

得到確切的異常的詳細信息,如該場失敗的約束使用ConstraintViolationException的getConstraintViolations()。此方法爲所有約束違規提供了一組。

0

您需要註冊就可以訪問:

public class MyApplication extends Application { 
    public Set<Class<?>> getClasses() { 
     Set<Class<?>> s = new HashSet<Class<?>>(); 
     s.add(HelloWorldResource.class); 

     /** you need to add ExceptionMapper class as well **/ 
     s.add(GenericExceptionMapper.class) 
     return s; 
    } 
} 
+0

映射器已被註冊! –

相關問題