I’m new to spring. I want to add a row in MySQL database by a post request using POSTMAN via Spring boot project.
I send a post request with a json body providing neccessary values for the table. but when I check the table I see that NULL values are inserted in table. Can you please help me prevent inserting null values in table.
CODE
AuthConroller.java Class
import com.hackernewsclient.com.hackernewsclient.dto.ResgisterRequest; import com.hackernewsclient.com.hackernewsclient.service.AuthService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api/auth") public class AuthController { @Autowired private AuthService authService; @PostMapping("/signup") public ResponseEntity signup(@RequestBody ResgisterRequest resgisterRequest) { authService.signup(resgisterRequest); return new ResponseEntity(HttpStatus.OK); } }
User.java entity class
import javax.persistence.*; @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String userName; @Column private String password; @Column private String email; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
AuthService.java class
import com.hackernewsclient.com.hackernewsclient.dto.ResgisterRequest; import com.hackernewsclient.com.hackernewsclient.model.User; import com.hackernewsclient.com.hackernewsclient.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class AuthService { @Autowired private UserRepository userRepository; public void signup(ResgisterRequest resgisterRequest) { User user = new User(); user.setUserName(resgisterRequest.getUsername()); user.setPassword(resgisterRequest.getPassword()); user.setEmail(resgisterRequest.getEmail()); userRepository.save(user); } }
UserRepository.java interface
import com.hackernewsclient.com.hackernewsclient.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User,Long> { }
RegisterRequest.java Data Object class for post request’s body
public class ResgisterRequest { private String user_name; private String password; private String email; public String getUsername() { return user_name; } public void setUsername(String username) { username = username; } public String getPassword() { return password; } public void setPassword(String password) { password = password; } public String getEmail() { return email; } public void setEmail(String email) { email = email; } }
Screenshot of POSTMAN – my request attempt which its status is successful
Screenshot of User table values in MySQL phpmyadmin
Intellij Log
Hibernate: insert into user (email, password, user_name) values (?, ?, ?)
Advertisement
Answer
You have issues in ResgisterRequest
setters. username = username;
assigns value to the same variable. It should be this.user_name = user_name;
class ResgisterRequest { private String user_name; private String password; private String email; public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }