Post方法@RequestBody对象参数null

时间:2017-03-05 21:50:30

标签: rest post spring-boot

我正在尝试从邮递员发布POST请求,并且正文中的json未转换为控制器中的java对象。

@RequestMapping(method = RequestMethod.POST , value="/register")
    public void saveUser(@RequestBody User user){
//user object getting created with all variables null
        manageUserService.saveUser(user);

    }

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.naga</groupId>
    <artifactId>MicroServices</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>MicroServices Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>1.5.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.7</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>MicroServices</finalName>
    </build>
</project>

在日志中我看到了

Hibernate:插入用户(zip,address1,address2,address3,city,email,first_name,last_name,password,state,user_name)值(?,?,?,?,?,?,?,?,? ,?,?)

不知道控制器中是否缺少某些内容或使用我的pom Postman request is sent like this

用户对象:

package com.naga.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Long userId;

    private String firstName;

    private String lastName;

    private String email;

    private String address1;

    private String address2;

    private String address3;

    private String city;

    private String state;

    private String Zip;

    private String userName;

    private String password;

    public final Long getUserId() {
        return userId;
    }

    public final void setUserId(Long userId) {
        this.userId = userId;
    }

    public final String getFirstName() {
        return firstName;
    }

    public final void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public final String getLastName() {
        return lastName;
    }

    public final void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public final String getEmail() {
        return email;
    }

    public final void setEmail(String email) {
        this.email = email;
    }

    public final String getAddress1() {
        return address1;
    }

    public final void setAddress1(String address1) {
        this.address1 = address1;
    }

    public final String getAddress2() {
        return address2;
    }

    public final void setAddress2(String address2) {
        this.address2 = address2;
    }

    public final String getAddress3() {
        return address3;
    }

    public final void setAddress3(String address3) {
        this.address3 = address3;
    }

    public final String getCity() {
        return city;
    }

    public final void setCity(String city) {
        this.city = city;
    }

    public final String getState() {
        return state;
    }

    public final void setState(String state) {
        this.state = state;
    }

    public final String getZip() {
        return Zip;
    }

    public final void setZip(String zip) {
        Zip = zip;
    }

    public final String getUserName() {
        return userName;
    }

    public final void setUserName(String userName) {
        this.userName = userName;
    }

    public final String getPassword() {
        return password;
    }

    public final void setPassword(String password) {
        this.password = password;
    }


}

在DB中,用户对象是持久的: user object in DB persisted but nothing in the fields

1 个答案:

答案 0 :(得分:0)

我相信你在实体上遗漏了一些东西

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

@Id 
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userId;

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

  .....
}

这应该对你有帮助。