Ich verwende @ExcpetionHandler
in meiner Java8-App. Ich kann diese Funktion nicht erreichen. Bei allen Ausnahmen ist die HTTP-Antwort immer 500 (mit der Nachricht, die ich in die Ausnahme eingefügt habe) und ignoriert die Methoden @ExceptionHandler
.Spring-Boot @ExceptionHandler in Java8 wird nicht erreicht
Wenn mein Kollege die App mit Java 7 Compiler ausführt, funktioniert es.
Warum kann die @ExceptionHandler-Annotation nicht mit Java 8 erreicht werden?
Controller:
validateInput()
ist InputParameterException
werfen. InputParameterException
ist im Versuch/Fang gefangen, der es erneut wirft.
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;
}
}
}
Relevante Teil von 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>
Dieser Code nicht zu @ExceptionHandler
mit Java8 zu erreichen. Beim Wechsel zu Java7, ohne Code-Änderungen, funktioniert es.
Update:
Die Fehlermeldung, die ich erhalten ist:
{
"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"
}
Ich entfernte @Controller
Annotation und ich habe auch versucht, Resolver zu verwenden, aber es hat nicht funktioniert.
-Code des Resolver, im selben Paket wie der Rest Controller-Klasse:
@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) {
.....
}
}
Warum haben Sie sowohl Controller als auch RestController? Haben Sie versucht, die Ausnahme-Handler in eine eigene Component-Klasse zu setzen? –
@ Shervin Asgari, ich möchte den ExceptionHandler im Controller haben. Ich habe nur 1 Controller und brauche keine andere Klasse. – KerenSi
Was ist * der eigentliche Fehler * Sie sehen, wenn Sie 500 haben? – Eugene