spring boot + spring data rest: post json 列 "first_name" 中的空值违反了非空约束

spring boot + spring data rest: post json null value in column "first_name" violates not-null constraint

我使用spring引导和spring数据休息来制作user,当我制作POST方法时进行这样的测试(使用 postman):

{
    "firstName": "Yue",
    "lastName": "Yang",
    "email": "g1enyy0ung@gmail.com",
    "password": "123"
}

我收到的回复是:

{
  "cause": {
     "cause": {
      "cause": null,
      "message": "ERROR: null value in column \"first_name\" violates      not-null constraint\n  Detail: Failing row contains (2, null, null, null, f, null, null)."
    },
    "message": "could not execute statement"
  },
  "message": "could not execute statement; SQL [n/a]; constraint [first_name]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
}

我对此很困惑,我不知道为什么 firstName 是空的, 这是我定义的实体:

@Entity
@Table(name = "app_user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Getter
    @Setter
    @Column(columnDefinition = "SERIAL")
    private Long id;

    @Column(name = "first_name")
    @Getter
    @Setter
    private String firstName;

    @Column(name = "last_name")
    @Getter
    @Setter
    private String lastName;

    @Getter
    @Setter
    private String email;

    @Column(name = "pass")
    @Getter
    @Setter
    private String password;

    @Getter
    private boolean admin;

    @Column(name = "last_login", columnDefinition = "TIMESTAMPTZ")
    @Temporal(TemporalType.TIMESTAMP)
    @Getter
    private Date lastLogin;

    public User() {}

    public User(String firstName, String lastName, String email, String password) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
    }

而SQL是(使用PostgreSQL,我也使用flyway来迁移table 当 spring 引导运行时):

CREATE TABLE "app_user" (
  id         SERIAL PRIMARY KEY,
  first_name VARCHAR(20) NOT NULL,
  last_name  VARCHAR(20) NOT NULL,
  email      VARCHAR(30) NOT NULL,
  admin      BOOLEAN DEFAULT FALSE,
  last_login TIMESTAMPTZ,
  pass       VARCHAR(32) NOT NULL
);

hibernate 总是执行这个 SQL:

Hibernate: insert into app_user (admin, email, first_name, last_login, last_name, pass) values (?, ?, ?, ?, ?, ?)

为什么会这样?我用这个问题搜索google,但我发现几乎所有搜索结果都使用嵌入数据库,比如H2.And几乎所有教程都不使用@Column注解,所以这个问题与Hibernate有关吗?我是 Hibernate 和 spring 引导的新手,现在我正在阅读文档以找到这个问题的解决方案。

希望有人能帮我改正这个question.Thanks!

更新:application.yml =>

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/thoughtplus
    username: g1eny0ung
    password: zhuzhu59y
    platform: POSTGRESQL
  jpa:
    hibernate:
      ddl-auto: validate
    show-sql: true
  data:
    rest:
      base-path: /api

server:
  port: 8888

我测试了两种构建方式 class.One 只有 运行 spring 启动应用程序并且会自动 build.The 其他是手动构建使用 Gradle build.

前面我查看反编译后的class时不会生成getter和setter方法。

第二个会生成成功

我猜是spring开机默认配置会忽略Lombok的注释。

如果我不正确,请告诉我并告诉我这个问题的真正原因。

更新:见@Barath 回答。

我尝试使用 Maven 构建并遇到了类似的问题。它在执行 maven build 后得到解决。

我认为问题不在于 spring 默认配置,因为我能够成功地从存储库中检索实体。但是当我将实体作为控制器的响应发送时,问题出在转换实体时 jackson 映射器。

示例:

      @GetMapping("/")
        public User getUser() throws JsonProcessingException{
            User user=userSer.getUser(100); 
            System.out.println(user.toString());    
            ObjectMapper mapper=new ObjectMapper();
            System.out.println("Object mapper "+mapper.writeValueAsString(user));

            return user;
        }

输出:

User [userId=100, userName=barath]
Object mapper {}

GitHub link : Spring-data-lombok