Spring boot 中带有 400 状态代码的错误请求

问题描述 投票:0回答:3

最近我正在使用 Spring boot 框架和 Kotlin。使用 GET 方法一切都OK。但是,当使用 POST 方法注册新用户时,我遇到了一些问题,状态代码为 400 的错误请求。 这是与我的 Spring Boot 项目关联的代码

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)
    }
}

我的请求负载是

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"
}

我的响应负载是

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
spring-boot rest kotlin
3个回答
5
投票

在 Spring Boot 配置中添加以下属性 logging.level.org.springframework.web=DEBUG您将在控制台日志上得到 400 Bad requests 的确切原因。


2
投票

您在实体中使用了 camelCase - 请求负载中使用了

User
snake_case。建议不要混合对象映射语法。你可以尝试一下这个请求负载吗:

{
    "firstName" : "Abid",
    "lastName" : "Affan",
    "email" : "[email protected]",
    "gender" : "M",
    "dateOfBirth" : "2019-05-03",
    "country" : "Bangladesh"
}

0
投票

如果您也不希望在客户端上有更清晰的错误消息(这在您无权访问日志或某些外部使用者调用此方法的情况下有意义),您可以通过添加以下内容来实现给你的

application.yaml
:

server:
  error:
    include-message: always

这会导致类似的结果

{
    ...,
    "status": 400,
    "error": "Bad Request",
    "message": "Required parameter 'firstName' is not present.",
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.