[英]Spring-boot @ExceptionHandler in Java8 is not reached out
我在我的 Java8 應用程序中使用@ExcpetionHandler
。 我無法使用此功能。 在所有異常情況下,HTTP 響應始終為 500(我在異常中插入了消息),並且它忽略了@ExceptionHandler
方法。
當我的同事使用 java 7 編譯器運行該應用程序時,它可以工作。
Java 8 無法使用 @ExceptionHandler 注釋的任何原因?
控制器:
validateInput()
正在拋出InputParameterException
。 InputParameterException
在 try/catch 中被捕獲,它再次拋出它。
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;
}
}
}
pom.xml 中的相關部分:
<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>
此代碼無法通過@ExceptionHandler
到 @ExceptionHandler。 當切換到 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) {
.....
}
}
為了使Spring Boot能夠使用您的處理程序,您必須將其放在單獨的類中並正確進行注釋。
@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
來注釋您的控制器類是一個有效的選擇。 選擇一個或另一個並相應地注釋您的異常處理程序類(@RestControllerAdvice
或@ControllerAdvice
)
問題是導入了錯誤的HttpServletRequest
類:
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
導入應為:
import javax.servlet.http.HttpServletRequest;
當我更改導入時, @ExceptionHandler
方法起作用。
如果您找不到錯誤“@ExceptionHandler 無法使用 java8+”的解決方案,那么您可能犯了和我一樣的錯誤。 我將我的異常處理程序類命名為“ExceptionHandler”,它與注釋 @ExceptionHandler 的名稱相同。 更正后,我的 IDE 能夠導入為“org.springframework.web.bind.annotation.ExceptionHandler;”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.