0

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
3
  • You should probably add some details about how you are calling your code and errors/responses you get back for the non working code. Commented Jan 8, 2020 at 14:14
  • Alright so you should probably say how this code is being served, like I have this running in Tomcat and when I do a GET request like the following my Java code works fine but when I do the request to the Kotlin code I get a response that looks like X. Put that somewhere in your question not in the comments here. Commented Jan 8, 2020 at 14:21
  • I'm add example request and response Commented Jan 8, 2020 at 14:21

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.