ResourceController 上的 NumberFormatException 无法从 String 转换为 Integer
NumberFormatException On ResourceController cannot cast from String to Integer
我有一个带有 get 方法的控制器,它接收 3 个参数作为 java.lang.Integer
@GetMapping(path = Constantes.CONSULTAR_NODO_SUCESOR)
public ConsultarNodoSucesorResponse consultarNodoSucesor(@RequestHeader HttpHeaders headerRequest,
@RequestParam(required = false) Integer idArbol,
@RequestParam(required = false) Integer nivelFormulario,
@RequestParam(required = false) Integer idSucesor,
@RequestParam(required = false) String rutaRelacion,
@RequestParam(required = false) String tipoRelacion,
HttpServletResponse headerResponse) {
ConsultarNodoSucesorRequest request = new ConsultarNodoSucesorRequest(idArbol, nivelFormulario, idSucesor,
rutaRelacion, tipoRelacion);
return service.consultarNodoSucesor(headerRequest, request, headerResponse);
}
假设服务器针对错误请求返回代码 400,下面是完整错误
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: \"4111111111\
我在参数 idArbol
中发送值 4111111111 但它不会工作,另一方面,当我发送相同的参数值如 1044444444 它工作正常。
为什么会这样? Integer
的最大值是多少
与值 1044444444
不同,值 4111111111
对于 int
(因此 Integer
)数据类型而言太大,因此被理解为 String
.该值超出了整数的允许范围。
int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1.
您可能想知道为什么 Spring 会这样。这是Spring几个注解的Type Conversion特性,其中String
是默认值:
Some annotated controller method arguments that represent String-based request input (such as @RequestParam
, @RequestHeader
, @PathVariable
, @MatrixVariable
, and @CookieValue
) can require type conversion if the argument is declared as something other than String.
For such cases, type conversion is automatically applied based on the configured converters. By default, simple types (int
, long
, Date
, and others) are supported. You can customize type conversion through a WebDataBinder
(see DataBinder
) or by registering Formatters with the FormattingConversionServic
e. See Spring Field Formatting.
我有一个带有 get 方法的控制器,它接收 3 个参数作为 java.lang.Integer
@GetMapping(path = Constantes.CONSULTAR_NODO_SUCESOR)
public ConsultarNodoSucesorResponse consultarNodoSucesor(@RequestHeader HttpHeaders headerRequest,
@RequestParam(required = false) Integer idArbol,
@RequestParam(required = false) Integer nivelFormulario,
@RequestParam(required = false) Integer idSucesor,
@RequestParam(required = false) String rutaRelacion,
@RequestParam(required = false) String tipoRelacion,
HttpServletResponse headerResponse) {
ConsultarNodoSucesorRequest request = new ConsultarNodoSucesorRequest(idArbol, nivelFormulario, idSucesor,
rutaRelacion, tipoRelacion);
return service.consultarNodoSucesor(headerRequest, request, headerResponse);
}
假设服务器针对错误请求返回代码 400,下面是完整错误
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: \"4111111111\
我在参数 idArbol
中发送值 4111111111 但它不会工作,另一方面,当我发送相同的参数值如 1044444444 它工作正常。
为什么会这样? Integer
与值 1044444444
不同,值 4111111111
对于 int
(因此 Integer
)数据类型而言太大,因此被理解为 String
.该值超出了整数的允许范围。
int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1.
您可能想知道为什么 Spring 会这样。这是Spring几个注解的Type Conversion特性,其中String
是默认值:
Some annotated controller method arguments that represent String-based request input (such as
@RequestParam
,@RequestHeader
,@PathVariable
,@MatrixVariable
, and@CookieValue
) can require type conversion if the argument is declared as something other than String.For such cases, type conversion is automatically applied based on the configured converters. By default, simple types (
int
,long
,Date
, and others) are supported. You can customize type conversion through aWebDataBinder
(seeDataBinder
) or by registering Formatters with theFormattingConversionServic
e. See Spring Field Formatting.