gpt4 book ai didi

mysql - Hibernate 将 POST 请求中的 JSON 值错误地映射为 null

转载 作者:行者123 更新时间:2023-11-29 12:22:35 25 4
gpt4 key购买 nike

我将 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 参数值在数据库中错误地显示为 null。例如,当我发布以下 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 中得到以下绑定(bind)

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 期待的是名字、姓氏 ..

public void setFirstName( // should be setFirstname
public void setLastName( // should be setLastname

关于mysql - Hibernate 将 POST 请求中的 JSON 值错误地映射为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28797810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com