Spring's @RequestParam with Enum
<p>I have this enum :</p>

<pre><code>public enum SortEnum {
    asc, desc;
}
</code></pre>

<p>That I want to use as a parameter of a rest request :</p>

<pre><code>@RequestMapping(value = "/events", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List&lt;Event&gt; getEvents(@RequestParam(name = "sort", required = false) SortEnum sort) {
</code></pre>

<p>It works fine when I send these requests</p>

<pre><code>/events 
/events?sort=asc
/events?sort=desc
</code></pre>

<p>But when I send :</p>

<pre><code>/events?sort=somethingElse
</code></pre>

<p>I get a 500 response and this message in the console :</p>

<pre><code>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 = &lt;500 Internal Server Error,com.myApp.web.rest.errors.ErrorVM@1e3343c9,{}&gt;
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
</code></pre>

<p>Is there a way to prevent spring from throwing these exceptions and set the enum to null ?</p>

<p><strong>EDIT</strong> </p>

<p>The Strelok's accepted answer works. However, I decided to deal with handling the MethodArgumentTypeMismatchException.</p>

<pre><code>@ControllerAdvice
public class ExceptionTranslator {

    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public ResponseEntity&lt;Object&gt; handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
        Class&lt;?&gt; 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);
    }
</code></pre>