I am writing simple CRUD with Kotlin + Spring Boot + Hibernate and came up with the issue:
I have a field in User
class called age
and type Int
import javax.persistence.*
import javax.validation.constraints.NotBlank
import javax.validation.constraints.Size
@Entity
@Table(name = "users")
data class User(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,
@NotBlank
@Column(unique = true)
@Size(min = 1, max = 100)
val username: String,
@NotBlank
@Size(max = 50)
val firstName: String,
@Size(max = 50)
val lastName: String?,
@Size(min = 1, max = 100)
val age: Int
)
Then I have post request which creates a user
@PostMapping
fun post(@RequestBody user: User): User {
log.debug("POST: {}", user)
return userRepository.save(user)
}
The problem is that when I miss age
field in JSON it doesn't respond with 400 error, but defaults to 0
instead.
How do I make this field required ?