Recently I found myself needing to validate fields in a Spring Boot controller written in Kotlin. Here's the code I ended up with:
@Controller
@RequestMapping("api/sample")
class SampleController {
@PostMapping
fun post (@Valid @RequestBody request: SampleType){
return ResponseEntity.ok().build()
}
}
data class SampleType(@field:NotBlank info: String)
The key is the @field
in the data class. Without that, Kotlin will apply validation to the constructor parameters by default. This makes sense when you see it, but is less obvious when you're wading through your 10th Java based tutorial on Spring validation.
Hope this helps!
Share
Read More
01.24.2022 | Tutorial Culture | Peter Swartwout
If you've ever taken a fitness class, you've heard the instructor talk about consistency. It's not about how fast you pedal or how much weight you lift...
01.13.2022 | Culture | Kate Courtney
Pair programming is a way to develop software. It used to involve two software developers sitting down at one workstation and writing code together.
01.12.2022 | Blockchain Web3 Tutorial Backend | Austin Vance
The "_Non-Fungible_" attribute of an NFT has some remarkable properties that allow smart contract developers to store state in a permissionless manner and enable that...
01.03.2022 | Blockchain Testing Web3 | Austin Vance
There are two ways: you can deploy "mock contracts" or you can use a mocking library. There are tradeoffs, but for this post I am going to focus on using a Smock's...
06.30.2021 | Culture | Katy Scott
At Focused Labs, collaboration is key to how we work together; it helps our teams learn from each other, brings us closer and helps us become more efficient...
06.23.2021 | Culture | Austyn
Late-night feedings and diaper changes, the 3-4 month sleep regression, teething, and a growth spurt all mean I'm getting less sleep than...
05.12.2021 | Culture Backend Frontend | Ryan Taylor
Temporarily disrupts "normal" business operations and allow self-organized teams to rapid prototype around their interest areas