我是春天的新手。我想通过Spring Boot项目使用POSTMAN通过发布请求在MySQL数据库中添加一行。
我发送带有json正文的发布请求,该声明为表提供了必要的值。但是当我检查表时,我看到在表中插入了NULL值。您能帮我防止在表中插入空值吗?
CODE
AuthConroller.java类
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实体类
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类
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;@Servicepublic 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接口
import com.hackernewsclient.com.hackernewsclient.model.User;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.stereotype.Repository;@Repositorypublic interface UserRepository extends JpaRepository<User,Long> {}
PostRequest正文的RegisterRequest.java数据对象类
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; }}
POSTMAN的屏幕快照-我的请求尝试(其状态为成功)
在MySQL中的用户表值的屏幕快照phpmyadmin
Intellij Log
Hibernate: insert into user (email, password, user_name) values (?, ?, ?) 您在ResgisterRequest二传手中遇到问题。username = username;将值分配给同一变量。它应该是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; }}
首先,您可以重新截图吗?它是不可见的。
请更正ResgisterRequest.java中的设置器。该方法中的值应为:this.password = password; 同样。请更正所有设置者。
在ResgisterRequest.java中将变量名称保留为userName,然后再次生成getter / setter。您可以从eclipse源菜单中生成getter和setter来避免此类错误。
您可以在AuthController方法中设置调试并检查resgisterRequest的值吗?