【问题标题】:Bad Request with 400 status code in Spring bootSpring boot中带有400状态码的错误请求
【发布时间】:2021-05-30 16:40:57
【问题描述】:

最近我正在使用带有 Kotlin 的 Spring Boot 框架。使用 GET 方法一切正常。但是在使用 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" : "aminul15-5281@diu.edu.bd",
    "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


    【解决方案1】:

    在您的 Spring Boot 配置中添加以下属性 logging.level.org.springframework.web=DEBUG 您将获得控制台日志中收到 400 个错误请求的确切原因。

    【讨论】:

      【解决方案2】:

      您在您的实体中使用了 camelCase - User 和您的请求负载中的 snake_case。建议不要混合对象映射的语法。你能试试这个请求负载吗:

      {
          "firstName" : "Abid",
          "lastName" : "Affan",
          "email" : "aminul15-5281@diu.edu.bd",
          "gender" : "M",
          "dateOfBirth" : "2019-05-03",
          "country" : "Bangladesh"
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-24
        • 2017-12-20
        • 2016-04-20
        • 1970-01-01
        • 2017-08-10
        • 1970-01-01
        • 2020-10-21
        • 2019-09-13
        相关资源
        最近更新 更多