2016-07-22 15 views
0

フィールドを検証するサービスを作成しています。フィールドが自分のルールで正常でない場合、私はUnprocessableエンティティを処理するために作成したクラスであるResourceBusinessExceptionをスローします。ただし、この例外がスローされると、SpringMenuProcessorクラスでNullPointerExceptionがスローされます。SpringクラスのNullPointerException AbstractMessageConverterMethodProcessor

私が話している内容を読みやすくするために、この問題に関連するすべてのコードを添付します。よりシンプルにするために、私はコードの例外を余儀なくされました。

まず第一に、私の春バージョン:

<spring-framework-version>4.2.6.RELEASE</spring-framework-version> 

私のコントローラの方法:

/** 
* @param MyType 
* @return 
*/ 
@ApiOperation(value = "", notes = "", authorizations = { @Authorization(value = ApplicationSwaggerConfig.securityOAuth2, scopes = { @AuthorizationScope(scope = WRITE_SCOPE, description = WRITE_DESCRIPTION) }) }) 
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 201, message = "Created"), @ApiResponse(code = 401, message = "Unauthorized"), 
@ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 404, message = "Not Found"), @ApiResponse(code = 422, message = "Unprocessable Entity") }) 
@RequestMapping(value = "/xpto", method = POST, produces = { APPLICATION_XML_VALUE, APPLICATION_JSON_VALUE }) 
public ResponseEntity<Void> saveXPTO(@RequestBody MyType myType) { 
    Long tenantId = 1L; 
    MyClass myObject = service.findMyClass(tenantId, empresaType.getAttribute()); 
    MyOtherClass myOtherObject= this.applyForOne(myObject); 
    PreconditionsRest.checkCondition(false, "ERROR!!"); // This is where I'm forcing an error. 
    contribuinteService.saveMyOtherObject(myOtherObject); 
    return new ResponseEntity<Void>(new HttpHeaders(), CREATED); 
} 

マイサービス方法:

/** 
* Check condition. 
* 
* @param condition O(a)(s) condition 
*/ 
public static void checkCondition(boolean condition, String message){ 
    if (!condition) { 
     throw new ResourceBusinessException(message); 
    } 
} 

マイResourceBusinessExceptionクラス:

public class ResourceBusinessException extends RuntimeException{ 

private static final long serialVersionUID = 1L; 


private ErrorMessage errorMessage; 


public ResourceBusinessException() { 
    super(); 
} 


public ResourceBusinessException(String message, Throwable cause,boolean enableSuppression, boolean writableStackTrace) { 
    super(message, cause, enableSuppression, writableStackTrace); 
} 


public ResourceBusinessException(String message, Throwable cause) { 
    super(message, cause); 
} 


public ResourceBusinessException(String message) { 
    super(message); 
} 


public ResourceBusinessException(Throwable cause) { 
    super(cause); 
} 


public ResourceBusinessException(ErrorMessage errorMessage){ 
    super(errorMessage.getDetail()); 
    this.errorMessage = errorMessage; 
} 


public ErrorMessage getErrorMessage() { 
    return errorMessage; 
} 

}

マイResourceBusinessExceptionHandlerクラス:

public class ResourceBusinessExceptionHandler extends ErrorMessageRestExceptionHandler<ResourceBusinessException>{ 


private static final Logger LOGGER = LoggerFactory.getLogger(ResourceBusinessExceptionHandler.class); 

private ErrorMessage errorMessage; 

public ResourceBusinessExceptionHandler() { 
    super(UNPROCESSABLE_ENTITY); 
} 


@Override 
public ErrorMessage createBody(ResourceBusinessException ex, HttpServletRequest req) { 
    LOGGER.debug("ResourceBusinessExceptionHandler - Started"); 
    ErrorMessage tmpl = super.createBody(ex, req); 
    ValidationErrorMessage msg = new ValidationErrorMessage(tmpl); 
    errorMessage=ex.getErrorMessage(); 
    if(errorMessage==null){ 
     errorMessage = new ErrorMessage(); 
    } 
    msg.setType(ResourceHandlerUtil.getType(errorMessage,UNPROCESSABLE_ENTITY_SPEC)); 
    msg.setTitle(ResourceHandlerUtil.getTitle(errorMessage,HttpStatus.UNPROCESSABLE_ENTITY)); 
    msg.setDetail(ResourceHandlerUtil.getDetail(errorMessage,HttpStatus.UNPROCESSABLE_ENTITY)); 
    msg.setInstance(ResourceHandlerUtil.getUriInstance(errorMessage,UNPROCESSABLE_ENTITY_URI)); 
    LOGGER.debug("ResourceBusinessExceptionHandler - Complete"); 
    return msg; 
} 

マイコンフィギュレーション豆:私は私のサービスへの要求を行うとき

<bean id="compositeExceptionResolver" class="org.springframework.web.servlet.handler.HandlerExceptionResolverComposite"> 
    <property name="order" value="0" /> 
    <property name="exceptionResolvers"> 
     <list> 
      <ref bean="exceptionHandlerExceptionResolver" /> 
      <ref bean="restExceptionResolver" /> 
     </list> 
    </property> 
</bean> 

    <bean id="restExceptionResolver" class="cz.jirutka.spring.exhandler.RestHandlerExceptionResolverFactoryBean"> 
    <property name="messageSource" ref="httpErrorMessageSource" /> 
    <property name="defaultContentType" value="application/json" /> 
    <property name="exceptionHandlers"> 
     <map> 
      <!-- 422 --> 
      <entry key="com.company.project.exception.ResourceBusinessException"> 
       <bean class="com.company.project.exception.handler.ResourceBusinessExceptionHandler"/> 
      </entry> 
     </map> 
    </property> 
</bean><bean id="exceptionHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"/> 

<bean id="httpErrorMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" p:defaultEncoding="UTF-8"/> 

はその後、次のスタックトレースが私のコンソールに表示されます:

15:26:09.119 [ht tp-nio-8013-exec-3] DEBUG c.j.s.e.h.RestExceptionHandler - POST/api/xpto〜> 422 com.company.project.exception.ResourceBusinessException:エラー!

[...]そして、NullPointerExceptionが...

ERROR cjseRestHandlerExceptionResolverは - エラー応答を処理するのに失敗しました:< 422処理不可能エンティティ、ValidationErrorMessage(エラー= []){} > java.lang.NullPointerException:null 、org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.getGenericType(AbstractMessageConverterMethodProcessor.java:271)〜[spring-webmvc-4.2.6.RELEASE.jar:4.2 .6RELEASE] at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverter MethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:172)〜[ばねwebmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValueで ( HttpEntityMethodProcessor.java:183)〜[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] (cz.jirutka.spring.exhandler.RestHandlerExceptionResolver.processResponse(RestHandlerExceptionResolver.java:141)〜[spring- [rest-exception-handler-1.0.3.jar:1.0.3] をcz.jirutka.spring.exhandler.RestHandlerExceptionResolver.doResolveException(RestHandlerExceptionResolver.java:107)〜[spring-rest-exception-handler-1.0.3。 jar:1.0.3] at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:137)[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org。SpringFramework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1185)[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet .java:1022)[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:973)[spring-webmvc-4.2 .6RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)[spring-webmvc-4.2.6.RELEASE.jar:4.2.6。 RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.web.servlet .FrameworkServlet.doPost(FrameworkServlet.java:869)[spring-webmvc-4.2.6.RELEASE。 jar:4.2.6.RELEASE] (javax.servlet.http.HttpServlet.service(HttpServlet.java:648)[servlet-api.jar:na] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet .java:843)[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] のjavax.servlet.http.HttpServlet.service(HttpServlet.java:729)[servlet-api.jar:na ] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)[catalina.jar:8.0.36] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.36] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)[tomcat-websocket.jar:8.0.36] at org.apache.catalina。 core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)[c atalina.jar:8.0.36] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)[catalina.jar:8.0.36] at org.apache.catalina.core.StandardWrapperValve.invoke (StandardWrapperValve.java:212)[catalina.jar:8.0.36] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)[catalina.jar:8.0.36] at org.apache .catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)[catalina.jar:8.0.36] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)[catalina.jar:8.0 .36] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)[catalina.jar:8.0.36] at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java: 616)[catal ina.jar:8.0.36] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)[catalina.jar:8.0.36] at org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:528)[catalina.jar:8.0.36] at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)[tomcat-coyote.jar:8.0.36] at org .apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:670)[Tomcat-coyote.jar:8.0.36] at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java: 1520)[tomcat-coyote.jar:8.0.36] at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1476)[tomcat-coyote.jar:8.0.36] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor。 java:1142)[na:1.8.0_65] at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)[na:1.8.0_65] at org.apache.tomcat.util.threads。 TaskThread $ WrappingRunnable.run(TaskThread.java:61)[tomcat-util.jar:8.0.36] でjava.lang.Thread.run(Thread.java:745)[なし:1.8。0_65】以下

UPDATE

は、クラスRestHanlderExceptionResolverの方法processResponseを、次のとおりです。

protected void processResponse(ResponseEntity<?> entity, NativeWebRequest webRequest) throws Exception { 

    ModelAndViewContainer mavContainer = new ModelAndViewContainer(); 
    try { 
     responseProcessor.handleReturnValue(entity, null, mavContainer, webRequest); 

    } catch (HttpMediaTypeNotAcceptableException ex) { 
     LOG.debug("Requested media type is not supported, falling back to default one"); 
     fallbackResponseProcessor.handleReturnValue(entity, null, mavContainer, webRequest); 
    } 
} 
+0

RestHandlerExceptionResolverクラスを投稿してください - cz.jirutka.spring.exhandler.RestHandlerExceptionResolver.processResponse(RestHandlerExceptionResolver.java:141)〜[spring-rest-exception-handler-1.0.3.jar:1.0.3] –

答えて

0

私は問題を発見しました。私がSpringを使っているバージョンは、spring-rest-exception-handler-versionと互換性がありません。私が使用している残りの例外ハンドラは、このgithubプロジェクトからのものです:https://github.com/jirutka/spring-rest-exception-handler

spring 4.2.6 RELEASEバージョンを使用している場合は、spring-rest-exception-handlerのバージョンを1.2.0に更新する必要があります。これはNullPointerExceptionを解決するはずです。

関連する問題