Java 为什么';列不能为空';在post api期间,当我从html表单传递值时

Java 为什么';列不能为空';在post api期间,当我从html表单传递值时,java,hibernate,jakarta-ee,jax-rs,Java,Hibernate,Jakarta Ee,Jax Rs,当我试图通过运行我的应用程序来测试它时,导航到admin.html页面。我在表单中填写名字、姓氏和电子邮件的值。 单击“提交”按钮时,“列电子邮件不能为空”出现错误。 为了简洁起见,我排除了诸如getter、setter、constructor等代码。 这是我的admin.html页面,其中有一个表单,用于将值发布到我的api中,这些值用于创建employee对象 <form role="form" action="api/employees/create" method="post"&g

当我试图通过运行我的应用程序来测试它时,导航到admin.html页面。我在表单中填写名字、姓氏和电子邮件的值。 单击“提交”按钮时,“列电子邮件不能为空”出现错误。 为了简洁起见,我排除了诸如getter、setter、constructor等代码。 这是我的admin.html页面,其中有一个表单,用于将值发布到我的api中,这些值用于创建employee对象

<form role="form" action="api/employees/create" method="post">
    <div class="form-group">
        <label for="firstName">First Name</label>
        <input type="text" class="form-control" id="firstName" placeholder="Enter first name">
    </div>
    <div class="form-group">
        <label for="lastName">Last Name</label>
        <input type="text" class="form-control" id="lastName" placeholder="Enter last name">
    </div>
    <div class="form-group">
        <label for="email">Email</label>
        <input type="text" class="form-control" id="email" placeholder="Enter email">
    </div>
    <button type="submit" class="btn btn-success btn-block">Create</button>
</form>
这是我的Employee.java模型类——我有一个用于员工的构造函数,它只包含firstName、lastName和email,一个用于所有值

@XmlRootElement
@Entity
public class Employee {

@Id
@GeneratedValue
private int id;

@Expose
@Column(nullable = false)
private String firstName;

@Expose
@Column(nullable = false)
private String lastName;

@Expose
@Column(nullable = false, unique = true)
private String email;
这就是我在服务器端看到的错误

Hibernate: insert into Employee (availability_id, email, firstName, isAdmin, isManager, isMentee, isMentor, lastName, mentorDuration, topic_name, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2019-03-04 16:07:41 WARN  SqlExceptionHelper:129 - SQL Error: 1048, SQLState: 23000
2019-03-04 16:07:41 ERROR SqlExceptionHelper:131 - Column 'email' cannot be null
2019-03-04 16:07:41 INFO  AbstractBatchImpl:193 - HHH000010: On release of batch it still contained JDBC statements
2019-03-04 16:07:41 ERROR ExceptionMapperStandardImpl:39 - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]

在调试EmployeeAPI的过程中,值从前端以null形式出现


您是否尝试过调试和验证是否获得非空值?因为日志上说,
列“email”不能为null
。在您的api中,您没有验证收到的数据,您至少应该检查非null列,如firstName、lastName和email。@AndrianekenaMoise我在调试模式下启动了服务器,在整个api中都有断点,发现了我的问题。非常感谢。
Hibernate: insert into Employee (availability_id, email, firstName, isAdmin, isManager, isMentee, isMentor, lastName, mentorDuration, topic_name, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2019-03-04 16:07:41 WARN  SqlExceptionHelper:129 - SQL Error: 1048, SQLState: 23000
2019-03-04 16:07:41 ERROR SqlExceptionHelper:131 - Column 'email' cannot be null
2019-03-04 16:07:41 INFO  AbstractBatchImpl:193 - HHH000010: On release of batch it still contained JDBC statements
2019-03-04 16:07:41 ERROR ExceptionMapperStandardImpl:39 - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]