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

时间:2015-03-01 19:08:35

标签: mysql json spring hibernate jpa

我将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时,我看到firstname和lastname字符串被写入我的数据库为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    |                |
+-----------+---------------------+------+-----+---------+----------------+

是否有人知道可能导致此行为的原因。任何建议都会受到赞赏......我有一种感觉,这个人可能正在盯着我。

1 个答案:

答案 0 :(得分:1)

您的拼写错误/变量名称不匹配。

你的JSON

firstname
lastname

但是你的setter期待firstName,lastName ..

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