2017-05-15 4 views
0

Java8アプリケーションで@ExcpetionHandlerを使用しています。私はこの機能に手を差し伸べることができません。すべての例外を除いて、HTTP応答は常に500(例外に挿入されたメッセージとともに)であり、@ExceptionHandlerのメソッドは無視されます。Spring-boot Java8で@ExceptionHandlerに到達していません

私の同僚がJava 7コンパイラでアプリケーションを実行すると、動作します。

@ExceptionHandlerアノテーションがJava 8に到達できない理由は何ですか?

コントローラー:

validateInput()InputParameterExceptionを投げています。 InputParameterExceptionはtry/catchで捕捉され、try/catchは再びスローします。このコードはJava8で@ExceptionHandlerに手を差し伸べることができない

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-dependencies</artifactId> 
      <version>1.4.1.RELEASE</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
     <exclusions> 
      <exclusion> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-starter-logging</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-log4j2</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-jetty</artifactId> 
    </dependency> 
</dependencies> 

:のpom.xmlから

import org.apache.catalina.servlet4preview.http.HttpServletRequest; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ExceptionHandler; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.bind.annotation.ResponseStatus; 
import org.springframework.web.bind.annotation.RestController; 

import vce.exception.InputParameterException; 


@Controller 
@RestController 
public class VRestController { 


    @Resource(name="aManager") 
    AManager aManager; 

    public void setAManager(AManager aManager) { 
     this.aManager = aManager; 
    } 


    @RequestMapping(value ="/authentication/signature", method = RequestMethod.PUT) 
    public ResponseEntity<Object> signature(@RequestBody final Signature signature) { 

     return handle(Marker.UseCases.SIGNATURE, new Supplier<Integer>() { 

      @Override 
      public Integer get() { 
       validateInput(signature); 
       aManager.doSomething(signature); 
       return 0; 
      } 

     }); 
    } 


    private ResponseEntity<ErrorResponse> handle(UseCases useCase, Supplier<Integer> supplier) { 

     final Marker marker = Marker.REQUEST_MARKER.get(); 
     marker.startUseCase().setUseCase(useCase.name()).setStartTime(DateTime.now()); 
     try{ 
      marker.setSerializationId(serializationID); 

      supplier.get(); 
     } 

     catch (Exception e) { 
      marker.setException(e); 

      throw e; 
     } 

     finally { 
      ...... 
     } 

     return ResponseEntity.ok(new ErrorResponse(200,0,"Success")); 
    } 


    private static void validateInput(Signature signature) { 
     final String missingErr = "The request is missing the following parameter: "; 

     if(signature.getData()==null) { 
      throw new InputParameterException(missingErr+VConstants.DATA_FIELD_NAME, VErrorCodeEnum.MISSING_FIELD); 
     } 

     if(signature.getSignature()==null) { 
      throw new InputParameterException(missingErr+VConstants.SIGNATURE_FIELD_NAME, VErrorCodeEnum.MISSING_FIELD); 
     } 
    } 


    @ExceptionHandler(InputParameterException.class) 
    @ResponseStatus(value= HttpStatus.BAD_REQUEST) 
    @ResponseBody 
    public ErrorResponse handleInputParameterException(HttpServletRequest req, InputParameterException e) { 
     ..... 
     return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getErrorCode().getCode(), e.getMessage()); 
    } 


    @ExceptionHandler(Exception.class) 
    @ResponseStatus(value= HttpStatus.INTERNAL_SERVER_ERROR, reason="Internal Server Error - Any Exception") 
    public void handleAnyException(HttpServletRequest req, Exception e) { 

     ..... 
    } 

    static class ErrorResponse { 

     public int statusCode; 
     public int errorCode; 
     public String message; 

     public ErrorResponse(int statusCode, int errorCode, String message) { 
      this.statusCode = statusCode; 
      this.errorCode = errorCode; 
      this.message = message; 
     } 
    } 
} 

関連部分。コードを変更せずにJava7に切り替えると動作します。

更新:

私はエラーメッセージがある:

{ 
    "timestamp": 1494863566131, 
    "status": 500, 
    "error": "Internal Server Error", 
    "exception": "com.nds.ch.vge.vaus.exception.InputParameterException", 
    "message": "org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.nds.ch.vge.vaus.exception.InputParameterException: The request is missing the following parameter: data", 
    "path": "/authentication/signature" 
} 

私は@Controller注釈を削除し、私はまた、リゾルバを使用しようとしましたが、それはうまくいきませんでした。残りのコントローラクラスと同じパッケージでレゾルバの

コード、:春ブーツ用

@RestControllerAdvice 
@EnableWebMvc 
public class GlobalControllerExceptionHandler { 


     @ExceptionHandler(InputParameterException.class) 
     @ResponseStatus(value= HttpStatus.BAD_REQUEST) 
     public ErrorResponse handleInputParameterException(HttpServletRequest req, InputParameterException e) { 
      ..... 
      return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getErrorCode().getCode(), e.getMessage()); 
     } 


     @ExceptionHandler(Exception.class) 
     @ResponseStatus(value= HttpStatus.INTERNAL_SERVER_ERROR, reason="Internal Server Error - Any Exception") 
     public void handleAnyException(HttpServletRequest req, Exception e) { 

      ..... 
     } 
} 
+0

なぜコントローラとRestControllerの両方がありますか?例外ハンドラを独自のComponentクラスに入れてみましたか? –

+0

@Shervin Asgari、コントローラーの中にExceptionHandlerを持っています。私は唯一のコントローラと異なるクラスを使用する必要はありません。 – KerenSi

+0

*実際のエラー*あなたは500を持っているときに表示されますか? – Eugene

答えて

0

import org.apache.catalina.servlet4preview.http.HttpServletRequest; 

インポートする必要があります:

import javax.servlet.http.HttpServletRequest; 

私が変わったときインポート、@ExceptionHandlerメソッドが働いた。

0

あなたが別のクラスに入れてそれを適切に注釈を付ける必要があり、あなたのハンドラをピックアップします。

@RestControllerAdvice 
@EnableWebMvc 
public class GlobalControllerExceptionHandler { 

    @ExceptionHandler(value = { NoHandlerFoundException.class }) 
    @ResponseStatus(HttpStatus.NOT_FOUND) 
    public ApiErrorResponse noHandlerFoundException(Exception ex) { 
     return new ApiErrorResponse(404, 4041, ex.getMessage()); 
    } 
} 

また、私は同様@RestController@Controllerであなたのコントローラクラスに注釈を付けるための有効な選択肢ではないと思います。 1または別を選択し、それに応じて(@RestControllerAdviceまたは@ControllerAdviceあなたの例外ハンドラクラスに注釈を付ける)問題が間違っHttpServletRequestクラスをインポートした

+1

私はあなたが示唆したことを試しました。私は自分の投稿を更新しました。 – KerenSi

+0

これをapplication.propertiesに追加してください: 'spring.mvc.throw-exception-if-no-handler-found = true' – balag3

+0

は動作しません –

関連する問題