Reputation: 33
I'm have kotlin get request. Validation are not working, Possible to specify day of week more or less validate limits
@RestController
@Validated
open class GetCrab {
@GetMapping("/v1/get")
open fun getNameOfDayByNumber(@RequestParam dayOfWeek: @Min(1) @Max(7) Int?): String {
return "ok"
}
}
In the same java code validation works
@RestController
@Validated
public class GetCrab {
@GetMapping("/v1/get")
public String getNameOfDayByNumber(@RequestParam @Min(1) @Max(7) Integer dayOfWeek) {
return "ok";
}
}
Java code when validation works: request:
http://localhost:12722/v1/get?dayOfWeek=100
Response ->
{
"errors": [
{
"code": "INTERNAL_SERVER_ERROR",
"details": "getNameOfDayByNumber.dayOfWeek: must be less than or equal to 7"
}
]
}
Kotlin code, request http://localhost:12722/v1/get?dayOfWeek=100
Response:
ok
Upvotes: 0
Views: 537
Reputation: 3689
Please use open
modifier for methods too.
E.g. please try code:
@RestController
@Validated
open class GetCrab {
@GetMapping("/v1/get")
open fun getNameOfDayByNumber(@RequestParam dayOfWeek: @Min(1) @Max(7) Int?): String {
return "ok"
}
}
Both class and method should be open
(in Java terms - both of them shouldn't be final), because of Spring proxy logic. From linked article: Spring tries to inherit your class, because sometimes you can request exact your class from @Autowired parameter.
By default all classes and methods are not final in Java. However Kotlin classes/methods are final by default, so you need to put open
keyword before them to have ability to override.
Upvotes: 1