java后端参数默认值添加枚举_java-带枚举的Spring @RequestParam

在使用Spring MVC时,作者遇到一个问题,当REST请求的参数为枚举类型且值不匹配枚举时,Spring会抛出异常。文章讨论了如何防止这种异常并使枚举参数在不匹配时默认为null。通过添加一个全局异常处理器`ExceptionTranslator`,捕获`MethodArgumentTypeMismatchException`,并返回自定义的错误响应,而不是500状态码。
摘要由CSDN通过智能技术生成

我有这个枚举:

public enum SortEnum {

asc, desc;

}

我想用作rest请求的参数:

@RequestMapping(value = "/events", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

public List getEvents(@RequestParam(name = "sort", required = false) SortEnum sort) {

当我发送这些请求时,它工作正常

/events

/events?sort=asc

/events?sort=desc

但是当我发送:

/events?sort=somethingElse

我在控制台中收到500条响应和以下消息:

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,com.myApp.web.rest.errors.ErrorVM@1e3343c9,{}>

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

有没有一种方法可以防止spring抛出这些异常并将enum设置为null?

编辑

Strelok接受的答案有效。 但是,我决定处理MethodArgumentTypeMismatchException。

@ControllerAdvice

public class ExceptionTranslator {

@ExceptionHandler(MethodArgumentTypeMismatchException.class)

@ResponseBody

public ResponseEntity 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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值