0

I am trying to do a Spring MVC RestController with kotlin, but I am having an hard time with RequestParams with LocalDatetime

@GetMapping
fun getParams(@RequestParam(required = true) endDate: LocalDateTime)

If I don't specify the param localhost:8080/ it gives an error that can be caught in a ControllerAdvice but if I specify a empty value localhost:8080/endDate= it will give me java.lang.IllegalArgumentException: Parameter specified as non-null is null: method net.agroop.deviceapi.controllers.DeviceController.getAirParams, parameter endDate

I found no way to catch that error in the ControllerAdvice because it is a kotlin error, I think.

I tried with other types like Date and it also gives error. If I use String it works because String can be empty. Also, if I specify LocalDateTime? as nullable it works but then I have to catch the error in the Controller in every RequestMapping and not in the ControllerAdvice.

Woodchuck
  • 3,869
  • 2
  • 39
  • 70
Porfírio
  • 175
  • 1
  • 3
  • 11
  • Do you have an `@ControllerAdvice` set up? – Roddy of the Frozen Peas Jan 21 '19 at 19:10
  • Specify the param as nullable, or give it a default value in the annotation. – gidds Jan 21 '19 at 19:11
  • Don't use `@RequestParam` but rather use a form object which has a field `endDate` and use regular validation rules like `@NotNull` to validate the form. This also integrates with Spring I18N and you can provide nice error messages as well. – M. Deinum Jan 21 '19 at 19:11
  • Yes @RoddyoftheFrozenPeas i have it set up and it caches if i dont pass the parameter or an invalid date, but it will not if the parameter is passed but is empty/null – Porfírio Jan 21 '19 at 20:14
  • I know @gidds but i am trying to catch all errors in ControllerAdvice – Porfírio Jan 21 '19 at 20:15
  • I tried that @M.Deinum but it gives the same error, because kotlin have some form of null checking that i can't catch :( – Porfírio Jan 21 '19 at 20:16
  • Make the field optional instead of always required to be `null` then it should work. Unless Kotlin also interprets the `@NotNull` but I doubt that. – M. Deinum Jan 21 '19 at 20:17

2 Answers2

0

I figured it out myself. Just had to had a Converter<S, T> Component to handle the date conversion

@Component
class LocalDateConverter : Converter<String, LocalDateTime> {
    override fun convert(source: String): LocalDateTime = LocalDateTime.parse(source, DateTimeFormatter.ISO_DATE_TIME)
}

And now i can handle the exception using:

@ExceptionHandler(MethodArgumentTypeMismatchException::class)
fun handleMethodArgumentTypeMismatch(ex: MethodArgumentTypeMismatchException, request: WebRequest) =
Porfírio
  • 175
  • 1
  • 3
  • 11
0

To get out of the exception, just make the attribute of your model null, that is, endDate: LocalDateTime?

@GetMapping
fun getParams(@RequestParam(required = true) endDate: LocalDateTime?)

And to validate empty values you can use @field:NotEmpty in your data class with Hibernate Validator.

GL

Source

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62