I am building java application using Spring Boot 2.7.12 And I have very simpler REST API controller like that:

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


@RestController
@RequestMapping("/sample")
class SampleController {

    data class Arguments (
        val intValue: Int
    )


    @GetMapping("/echo")
    fun resource(args: Arguments) = "Return back: ${args.intValue}"
}

And the issue when I am making a regular regular request as expected, everything works fine:

$ curl http://localhost:8080/sample/echo?intValue=111
Return back: 111

However if I will enter non-numeric value, Spring Boot is crashing:

$ curl http://localhost:8080/sample/echo?intValue=111aaa
{"timestamp":"2023-07-27T16:03:34.023+00:00","path":"/sample/echo","status":500,"error":"Internal Server Error","requestId":"de11f158-6"}

If it is non-valid input, it should rather be 400 Bad Requests, than 500 Internal errors, and such things should be handled by the framework.

We using the Spring Boot framework, because we need some framework that will handle all edge case scenarios with mapping of the HTTP request parameters to controller input arguments, and that obviously should be handled properly by the framework.

Could you please advise, how to handle such HTTP requests properly?

Comment From: amidukr

As a workaround, I could use Java POJO instead of kotlin data class, and with use of POJO it returns 400 HTTP response code for non-numeric inputs, so it seems that issue only with Kotlin data class. However, I am looking for a cleaner way of doing things, where I don't duplicate into POJO content to enable use of kotlin data class.

@RestController
@RequestMapping("/sample")
class SampleController {

    data class Arguments(val intValue: Int) {
        class  Pojo{
            var intValue: Int = -1

            fun toData() = Arguments(intValue =  this.intValue)
        }
    }

    @GetMapping("/echo")
    fun resource(args: Arguments.Pojo) = "Return back: ${args.toData().intValue}"
}

Comment From: sdeleuze

I can't reproduce with Spring Boot 2.7.12 and Kotlin 1.8.22. Make sure you have com.fasterxml.jackson.module:jackson-module-kotlin and org.jetbrains.kotlin:kotlin-reflect dependencies on the classpath.

Comment From: amidukr

Hi @sdeleuze ,

  • All you recommendation are there.
  • And I can assure you, that issue is reproducible.

I didn't find how to reopen this ticket, so I created a new one: https://github.com/spring-projects/spring-framework/issues/31045, please check all steps, logs outputs, commands to run, and entire source code is there.

Comment From: sdeleuze

Next time I would suggest to just comment on the existing issue, add a repro, and I would have reopen this issue, but let's continue the conversation in the more complete new issue you have created.