I have this controller with a get method that recive 3 paramaters as 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);
}
When cosuming the server is returning code 400 for bad request, full error below
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\
I'm sending in the paramter idArbol
the value 4111111111 but It wont work, in the other hand when I send in that same paramters values like 1044444444 It works fine.
Why is this a thing? what is the max value of a Integer
Unlike the value 1044444444
the value 4111111111
is too large for the int
(and therefore Integer
) data type, therefore is understood as String
. The value overflows the allowed range for the integer.
See Primitive Data Types:
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.
You might wonder why Spring behaves like that. This is the Type Conversion feature of several annotations of Spring where String
is the default value:
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.