Recently I am using Spring boot framework with Kotlin. Every thing is all okey with GET method. But when registering a new user with POST method I go faced some problem having Bad Request with status code 400. Here is my code associate with my spring boot project
User.kt
@Entity
@Table(name = "user_info")
data class User(
@Id
@SequenceGenerator(
name = "user_seq",
sequenceName = "user_seq",
allocationSize = 1
)
@GeneratedValue(
strategy = SEQUENCE,
generator = "user_seq"
)
@Column(
name = "id",
updatable = false
)
val id: Long = -1,
@Column(
name = "first_name",
nullable = false,
length = 50,
updatable = true
)
val firstName: String,
@Column(
name = "last_name",
nullable = false,
length = 50,
updatable = true
)
val lastName: String,
@Column(
name = "email",
nullable = true,
length = 150,
updatable = true
)
val email: String,
@Column(
name = "gender",
nullable = false,
length = 2,
updatable = true
)
val gender: String,
@Column(
name = "date_of_birth",
nullable = false,
updatable = true
)
val dateOfBirth: LocalDate,
@Column(
name = "country",
nullable = false,
length = 50,
updatable = true
)
val country: String
)
UserController.kt
@RestController
@RequestMapping(
path = [
"/api/v1/"
]
)
class UserController(
@Autowired private val userService: UserService
) {
@PostMapping("register")
fun registerUser(@RequestBody user: User) {
userService.registerUser(user)
}
@GetMapping("users")
fun getUsers(): List<User> {
return userService.getUsers()
}
@GetMapping("user/{id}")
fun getUsers(@PathVariable("id") id: Long): User {
return userService.getUserInfo(id)
}
}
My Request Payload is
POST http://localhost:8080/api/v1/register
Content-Type: application/json
{
"first_name" : "Abid",
"last_name" : "Affan",
"email" : "[email protected]",
"gender" : "M",
"date_of_birth" : "2019-05-03",
"country" : "Bangladesh"
}
and my response payload is
POST http://localhost:8080/api/v1/register
HTTP/1.1 400
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 01 Mar 2021 05:52:03 GMT
Connection: close
{
"timestamp": "2021-03-01T05:52:03.634+00:00",
"status": 400,
"error": "Bad Request",
"message": ""JSON parse error: Instantiation of [simple type, class com.example.demo.user.User] value failed for JSON property firstName due to missing (therefore NULL) value for creator parameter firstName which is a non-nullable type; nested exception is com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class com.example.demo.user.User] value failed for JSON property firstName due to missing (therefore NULL) value for creator parameter firstName which is a non-nullable type\n at [Source: (PushbackInputStream); line: 8, column: 1] (through reference chain: com.example.demo.user.User[\"firstName\"])",
"path": "/api/v1/register"
}
Response code: 400; Time: 214ms; Content length: 119 bytes
Add following property in your spring boot configuration logging.level.org.springframework.web=DEBUG you will get the exact reason for getting 400 Bad requests on console logs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With