Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何配置DropWizard来解释请求失败?_Java_Dropwizard - Fatal编程技术网

Java 如何配置DropWizard来解释请求失败?

Java 如何配置DropWizard来解释请求失败?,java,dropwizard,Java,Dropwizard,如果发送JSON时没有请求正文: POST /stuff Content-Type: application/json Content-Length: 0 <---- Body missing! 现在,日志中只有一行: 127.0.0.1 - - [04/May/2015:13:21:25 +0000] "POST /stuff HTTP/1.1" 422 370 232 232 HTTP 422并不是一个不合理的暗示,但我必须相信这里有一

如果发送JSON时没有请求正文:

POST /stuff
Content-Type: application/json
Content-Length: 0
                       <---- Body missing!
现在,日志中只有一行:

127.0.0.1 - - [04/May/2015:13:21:25 +0000] "POST /stuff HTTP/1.1" 422 370 232 232
HTTP 422并不是一个不合理的暗示,但我必须相信这里有一个更详细的信息。类似地,如果缺少标头或路径组件与提供的格式不匹配,我希望看到类似“header\uuuuuuuuuuuu无法解析”这样的日志行


这样的日志记录是否内置于DropWizard中?如何启用它?

在这种情况下,您需要覆盖异常映射程序。在422情况下,您需要为ConstraintViolationException实现一个异常映射器


请参阅此答案:

默认情况下,Dropwizard/Jersey将异常映射到响应(默认异常映射器),在某些情况下,日志记录是有限的,因此不是很有用;因为简单,或者仅仅是为了迫使开发人员自己做更好的日志记录。请参阅更完整的说明:

Jersey,或者我应该说JAX-RS,公开了一种机制,允许您将抛出的异常或可丢弃的映射到REST响应,而不是未经处理并以stacktrace或错误文本的形式呈现给用户(这种机制需要您实现通用ExceptionMapper接口,然后注册它)这对于喜欢在使用API时将错误返回给客户机的RESTAPI来说是非常好的,比如返回可以在客户机上解析和处理的异常的JSON表示。“()

通过编程,可以通过以下步骤创建自己的自定义异常映射器。代码正在考虑Dropwizard 0.9.x,以及您问题中的具体情况;然而,处理其他情况/例外情况的过程是类似的。此外,为了简单起见,省略了一些行):

  • 禁用默认异常映射程序:

    public class CustomConstraintViolationExceptionMapper extends ConstraintViolationExceptionMapper
    {
        @Override
        public Response toResponse( ConstraintViolationException exception )
        {
            Response superResponse = super.toResponse( exception );
            ValidationErrorMessage validationErrorMessage = (ValidationErrorMessage) superResponse.getEntity();
            log.error( String.format( "%s\t%s",
                                  exception.getMessage(),
                                  String.join( ";", validationErrorMessage.getErrors() ) ) );
    
            return superResponse;
        }
    }
    
    public class CustomJsonProcessingExceptionMapper extends JsonProcessingExceptionMapper
    {
        @Override
        public Response toResponse( JsonProcessingException exception )
        {
             log.error( String.format( "%s", exception.getOriginalMessage() ) );
             return super.toResponse( exception );
        }
    }
    
    environment.jersey().register( new LoggingExceptionMapper<Throwable>(){} );
    environment.jersey().register( new EarlyEofExceptionMapper() );
    
    ERROR [2016-07-05 13:06:36,690] x.y.CustomConstraintViolationExceptionMapper: The request entity had the following errors:  field may not be empty
    ERROR [2016-07-05 13:19:41,326] x.y.CustomJsonProcessingExceptionMapper: Unrecognized field "UnknownField" (class x.y.z.Xyz), not marked as ignorable
    
    • 通过应用程序子类:

      AbstractServerFactory sf = (AbstractServerFactory) config.getServerFactory();
      sf.setRegisterDefaultExceptionMappers( false );
      
    • 通过应用程序配置:

      server:
          registerDefaultExceptionMappers: false
      
  • 通过添加日志记录(JSON和验证异常)创建自定义异常映射程序,以扩展原始异常映射程序:

    public class CustomConstraintViolationExceptionMapper extends ConstraintViolationExceptionMapper
    {
        @Override
        public Response toResponse( ConstraintViolationException exception )
        {
            Response superResponse = super.toResponse( exception );
            ValidationErrorMessage validationErrorMessage = (ValidationErrorMessage) superResponse.getEntity();
            log.error( String.format( "%s\t%s",
                                  exception.getMessage(),
                                  String.join( ";", validationErrorMessage.getErrors() ) ) );
    
            return superResponse;
        }
    }
    
    public class CustomJsonProcessingExceptionMapper extends JsonProcessingExceptionMapper
    {
        @Override
        public Response toResponse( JsonProcessingException exception )
        {
             log.error( String.format( "%s", exception.getOriginalMessage() ) );
             return super.toResponse( exception );
        }
    }
    
    environment.jersey().register( new LoggingExceptionMapper<Throwable>(){} );
    environment.jersey().register( new EarlyEofExceptionMapper() );
    
    ERROR [2016-07-05 13:06:36,690] x.y.CustomConstraintViolationExceptionMapper: The request entity had the following errors:  field may not be empty
    ERROR [2016-07-05 13:19:41,326] x.y.CustomJsonProcessingExceptionMapper: Unrecognized field "UnknownField" (class x.y.z.Xyz), not marked as ignorable
    
  • 注册您创建的自定义异常映射程序:

    environment.jersey().register( new CustomConstraintViolationExceptionMapper() );
    environment.jersey().register( new CustomJsonProcessingExceptionMapper() );
    
  • 不要忘记添加其他默认异常映射程序(如果需要-):

    public class CustomConstraintViolationExceptionMapper extends ConstraintViolationExceptionMapper
    {
        @Override
        public Response toResponse( ConstraintViolationException exception )
        {
            Response superResponse = super.toResponse( exception );
            ValidationErrorMessage validationErrorMessage = (ValidationErrorMessage) superResponse.getEntity();
            log.error( String.format( "%s\t%s",
                                  exception.getMessage(),
                                  String.join( ";", validationErrorMessage.getErrors() ) ) );
    
            return superResponse;
        }
    }
    
    public class CustomJsonProcessingExceptionMapper extends JsonProcessingExceptionMapper
    {
        @Override
        public Response toResponse( JsonProcessingException exception )
        {
             log.error( String.format( "%s", exception.getOriginalMessage() ) );
             return super.toResponse( exception );
        }
    }
    
    environment.jersey().register( new LoggingExceptionMapper<Throwable>(){} );
    environment.jersey().register( new EarlyEofExceptionMapper() );
    
    ERROR [2016-07-05 13:06:36,690] x.y.CustomConstraintViolationExceptionMapper: The request entity had the following errors:  field may not be empty
    ERROR [2016-07-05 13:19:41,326] x.y.CustomJsonProcessingExceptionMapper: Unrecognized field "UnknownField" (class x.y.z.Xyz), not marked as ignorable
    
  • 有关该主题的更多信息,您还可以参考以下参考资料: