Ich habe dieses Enum:Spring @RequestParam mit Enum
public enum SortEnum {
asc, desc;
}
Dass ich als Parameter einer Ruhe Anforderung verwenden möchten:
@RequestMapping(value = "/events", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<Event> getEvents(@RequestParam(name = "sort", required = false) SortEnum sort) {
Es funktioniert gut, wenn ich diese Anfragen senden
/events
/events?sort=asc
/events?sort=desc
Aber wenn ich senden:
erhalte ich eine 500-Antwort und diese Nachricht in der Konsole:
2016-09-29 17:20:51.600 DEBUG 5104 --- [ XNIO-2 task-6] com.myApp.aop.logging.LoggingAspect : Enter: com.myApp.web.rest.errors.ExceptionTranslator.processRuntimeException() with argument[s] = [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type [java.lang.String] to required type [com.myApp.common.SortEnum]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam com.myApp.common.SortEnum] for value 'somethingElse'; nested exception is java.lang.IllegalArgumentException: No enum constant com.myApp.common.SortEnum.somethingElse]
2016-09-29 17:20:51.600 DEBUG 5104 --- [ XNIO-2 task-6] com.myApp.aop.logging.LoggingAspect : Exit: com.myApp.web.rest.errors.ExceptionTranslator.processRuntimeException() with result = <500 Internal Server Error,[email protected],{}>
2016-09-29 17:20:51.601 WARN 5104 --- [ XNIO-2 task-6] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type [java.lang.String] to required type [com.myApp.common.SortEnum]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam com.myApp.common.SortEnum] for value 'somethingElse'; nested exception is java.lang.IllegalArgumentException: No enum constant com.myApp.common.SortEnum.somethingElse
Gibt es eine Möglichkeit Feder zu verhindern, dass diese Ausnahmen zu werfen und stellen Sie die Enum auf null?
EDIT
Die Arbeiten des Strelok akzeptierte Antwort. Ich entschied mich jedoch, die Behandlung von MethodArgumentTypeMismatchException zu behandeln.
@ControllerAdvice
public class ExceptionTranslator {
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
@ResponseBody
public ResponseEntity<Object> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
Class<?> type = e.getRequiredType();
String message;
if(type.isEnum()){
message = "The parameter " + e.getName() + " must have a value among : " + StringUtils.join(type.getEnumConstants(), ", ");
}
else{
message = "The parameter " + e.getName() + " must be of type " + type.getTypeName();
}
return buildResponse(HttpStatus.UNPROCESSABLE_ENTITY, message);
}
Dies ist th Die richtige Antwort, wenn Sie dieses Verhalten global für alle Endpunkte möchten. Wenn Sie dieses Verhalten nur für Ihren einen Controller wünschen, dann hat satish chennupati die richtige Lösung. – loesak
Danach wurde mein oauth2-Endpunkt irgendwie durcheinander gebracht und der Benutzer konnte sich nicht authentifizieren – Olayinka
Ist das 'com.fasterxml.jackson.databind.util.Converter'? –