来自 POST 的 Hibernate 映射 JSON 值错误地请求为 null
Hibernate mapping JSON values from POST request incorrectly as null
我将 Spring JPA 与 Hibernate 结合使用来创建一个服务,该服务仅从 POST 请求的正文中提取 JSON 数据,并将该数据写入mysql 数据库。
我已经实现了我的配置文件存储库,如下所示:
package oxi.repositories;
import oxi.models.*;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.*;
@RepositoryRestResource(collectionResourceRel="Profile", path="profile")
public interface ProfileRepository extends JpaRepository<Profile, Long>{
}
当我发送 POST 时,某些 JSON 参数值在数据库中错误地显示为空值。例如,当我 POST 以下 JSON 时,我看到名字和姓氏字符串作为 null
写入我的数据库
{
"firstname":"Foo",
"lastname":"Barnacles",
"email":"dmoney@dollabills.org",
"country":"USA",
"city":"ballin",
"age":45
}
我验证了我的 Apache 服务器正在获取正确的值
mod_dumpio: dumpio_in (data-HEAP): {\r\n "firstname":"Foo",\r\n "lastname":"Barnacles",\r\n "email":"dmoney@dollabills.org",\r\n "country":"USA",\r\n "city":"ballin",\r\n "age":45\r\n}
但是,我在 catalina.out
中得到以下绑定
sql.BasicBinder:81 - binding parameter [1] as [INTEGER] - [45]
sql.BasicBinder:81 - binding parameter [2] as [VARCHAR] - [ballin]
sql.BasicBinder:81 - binding parameter [3] as [VARCHAR] - [USA]
sql.BasicBinder:81 - binding parameter [4] as [VARCHAR] - [dmoney@dollabills.org]
sql.BasicBinder:69 - binding parameter [5] as [VARCHAR] - [null]
sql.BasicBinder:69 - binding parameter [6] as [VARCHAR] - [null]
我似乎找不到我的实体对象有什么问题
package oxi.models;
import javax.persistence.*;
import org.apache.log4j.Logger;
@Entity
public class Profile{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long user_id;
@Column(name="email")
private String email;
@Column(name="country")
private String country;
@Column(name="city")
private String city;
@Column(name="age")
private int age;
@Column(name="firstname")
private String firstname;
@Column(name="lastname")
private String lastname;
//Constructor
public Profile(){
}
//Setters
public void setFirstName(String firstname){
this.firstname = firstname;
}
public void setLastName(String lastname){
this.lastname = lastname;
}
public void setEmail(String email){
this.email = email;
}
public void setCountry(String country){
this.country = country;
}
public void setCity(String city){
this.city = city;
}
public void setAge(int age){
this.age = age;
}
//Getters
public String getFirstName(){
logger.trace("Set parameter firstname: " + this.firstname);
return this.firstname;
}
public String getLastName(){
logger.trace("Set parameter lastname: " + this.lastname);
return this.lastname;
}
public String getEmail(){
return this.email;
}
public String getCountry(){
return this.country;
}
public String getCity(){
return this.city;
}
public int setAge(){
return this.age;
}
}
而且我认为我的数据库格式正确(相对于我的实体对象)
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| user_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| firstname | tinytext | YES | | NULL | |
| lastname | tinytext | YES | | NULL | |
| email | tinytext | YES | | NULL | |
| country | tinytext | YES | | NULL | |
| city | tinytext | YES | | NULL | |
| age | smallint(6) | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
有谁知道可能导致这种行为的原因。任何建议将不胜感激...我有一种感觉,这个人可能正盯着我看。
您有拼写错误/变量名称不匹配。
你的JSON
firstname
lastname
但是您的 setter 需要 firstName, lastName ..
public void setFirstName( // should be setFirstname
public void setLastName( // should be setLastname
我将 Spring JPA 与 Hibernate 结合使用来创建一个服务,该服务仅从 POST 请求的正文中提取 JSON 数据,并将该数据写入mysql 数据库。
我已经实现了我的配置文件存储库,如下所示:
package oxi.repositories;
import oxi.models.*;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.*;
@RepositoryRestResource(collectionResourceRel="Profile", path="profile")
public interface ProfileRepository extends JpaRepository<Profile, Long>{
}
当我发送 POST 时,某些 JSON 参数值在数据库中错误地显示为空值。例如,当我 POST 以下 JSON 时,我看到名字和姓氏字符串作为 null
写入我的数据库{
"firstname":"Foo",
"lastname":"Barnacles",
"email":"dmoney@dollabills.org",
"country":"USA",
"city":"ballin",
"age":45
}
我验证了我的 Apache 服务器正在获取正确的值
mod_dumpio: dumpio_in (data-HEAP): {\r\n "firstname":"Foo",\r\n "lastname":"Barnacles",\r\n "email":"dmoney@dollabills.org",\r\n "country":"USA",\r\n "city":"ballin",\r\n "age":45\r\n}
但是,我在 catalina.out
中得到以下绑定sql.BasicBinder:81 - binding parameter [1] as [INTEGER] - [45]
sql.BasicBinder:81 - binding parameter [2] as [VARCHAR] - [ballin]
sql.BasicBinder:81 - binding parameter [3] as [VARCHAR] - [USA]
sql.BasicBinder:81 - binding parameter [4] as [VARCHAR] - [dmoney@dollabills.org]
sql.BasicBinder:69 - binding parameter [5] as [VARCHAR] - [null]
sql.BasicBinder:69 - binding parameter [6] as [VARCHAR] - [null]
我似乎找不到我的实体对象有什么问题
package oxi.models;
import javax.persistence.*;
import org.apache.log4j.Logger;
@Entity
public class Profile{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long user_id;
@Column(name="email")
private String email;
@Column(name="country")
private String country;
@Column(name="city")
private String city;
@Column(name="age")
private int age;
@Column(name="firstname")
private String firstname;
@Column(name="lastname")
private String lastname;
//Constructor
public Profile(){
}
//Setters
public void setFirstName(String firstname){
this.firstname = firstname;
}
public void setLastName(String lastname){
this.lastname = lastname;
}
public void setEmail(String email){
this.email = email;
}
public void setCountry(String country){
this.country = country;
}
public void setCity(String city){
this.city = city;
}
public void setAge(int age){
this.age = age;
}
//Getters
public String getFirstName(){
logger.trace("Set parameter firstname: " + this.firstname);
return this.firstname;
}
public String getLastName(){
logger.trace("Set parameter lastname: " + this.lastname);
return this.lastname;
}
public String getEmail(){
return this.email;
}
public String getCountry(){
return this.country;
}
public String getCity(){
return this.city;
}
public int setAge(){
return this.age;
}
}
而且我认为我的数据库格式正确(相对于我的实体对象)
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| user_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| firstname | tinytext | YES | | NULL | |
| lastname | tinytext | YES | | NULL | |
| email | tinytext | YES | | NULL | |
| country | tinytext | YES | | NULL | |
| city | tinytext | YES | | NULL | |
| age | smallint(6) | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
有谁知道可能导致这种行为的原因。任何建议将不胜感激...我有一种感觉,这个人可能正盯着我看。
您有拼写错误/变量名称不匹配。
你的JSON
firstname
lastname
但是您的 setter 需要 firstName, lastName ..
public void setFirstName( // should be setFirstname
public void setLastName( // should be setLastname