2017-03-05 78 views
1

我想從postman發佈一個POST請求,並且正文中的json不會被轉換爲控制器中的java對象。Post方法@RequestBody對象參數null

@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> 

在日誌中我看到

休眠:插入到用戶(郵政編碼,地址1,地址,2地址,城市,電子郵件,名字,姓氏,密碼,狀態,用戶名)值(?,?,?,?,?,?,?,?,?,?,?)

不知道是否在控制器中丟失了某些東西或與我的東西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

回答

0

我相信你缺少的東西在你的實體

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

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

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

    ..... 
} 

這應該是幫助你。

+0

Hi @Carlos。試過它沒有幫助,仍然無效 – juniorDeveloper

+0

我認爲'@ RequestBody'沒有必要。 – Carlos

+0

已經嘗試沒有它沒有幫助 – juniorDeveloper