spring - 如何在 Spring IOC 中使用 @Autowired 将值注入 Null 对象?
问题描述
我刚刚开始研究 Spring Rest 应用程序,目前我正在管理身份验证操作,我想做的是简单地检查输入的电子邮件地址是否已被使用,问题是当它不存在时我不能自动装配 bean用户我得到一个 NullPointerException。
我在 SOF 中发现了类似的问题,但我的代码似乎是正确的,请帮助。
这是我的代码:
项目架构
用户.java:
@Component
@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="idUser", scope = User.class)
public class User implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idUser;
@Column(name = "login", unique = true)
private String login;
private String password;
private String nom;
private String prenom;
private String email;
private long telephone;
private String statut;
private int isDeleted;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
private Role userRole;
public User(Long idUser, String login, String password, String nom, String prenom, String email,
long telephone, String statut, int isDeleted, Role userRole) {
super();
this.idUser = idUser;
this.login = login;
this.password = password;
this.nom = nom;
this.prenom = prenom;
this.email = email;
this.telephone = telephone;
this.statut = statut;
this.isDeleted = isDeleted;
this.userRole = userRole;
}
public User() {
super();
}
public Long getIdUser() {
return idUser;
}
public void setIdUser(Long idUser) {
this.idUser = idUser;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getTelephone() {
return telephone;
}
public void setTelephone(long telephone) {
this.telephone = telephone;
}
public String getStatut() {
return statut;
}
public void setStatut(String statut) {
this.statut = statut;
}
public int getIsDeleted() {
return isDeleted;
}
public void setIsDeleted(int isDeleted) {
this.isDeleted = isDeleted;
}
public Role getUserRole() {
return userRole;
}
public void setUserRole(Role userRole) {
this.userRole = userRole;
}}
用户存储库.java:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByLoginLikeAndPasswordLikeAndIsDeletedEquals(String login, String password, int i);
User findByLoginLike(String login);
}
用户DTO.java
@Component
public class UserDTO {
private Long idUser;
private String login;
private String password;
private String role ;
public UserDTO(Long idUser, String login, String password, String role) {
super();
this.idUser = idUser;
this.login = login;
this.password = password;
this.role = role;
}
public UserDTO() {
super();
}
public Long getIdUser() {
return idUser;
}
public void setIdUser(Long idUser) {
this.idUser = idUser;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
用户服务.java:
@Service
public class UserService {
@Autowired
private UserDTO userDto;
@Autowired
private User user;
@Autowired
private UserRepository userRep;
@Autowired
private RoleRepository roleRep;
public boolean addUser(String login, String password, String role) {
boolean state = false;
try {
System.out.println(user.toString());
user = userRep.findByLoginLike(login);
if (user == null) {
user.setPassword(password);
user.setLogin(login);
user.setUserRole(roleRep.findByNomRoleLike(role));
userRep.save(user);
state = true;
} else {
System.out.println("user already exists");
state = false;
}
} catch (Exception e) {
e.printStackTrace();
state = false;
}
return state;
}
用户控制器.java
@RestController
public class UserControler {
@Autowired
private User user;
@Autowired
private UserRepository userRep;
@Autowired
private List<User> allUsers;
@Autowired
private UserService userService;
@Autowired
private UserDTO userDTO;
@RequestMapping( value = "/addUser", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Boolean addUser(@RequestBody UserDTO u){
boolean state = false;
try {
state = userService.addUser(u.getLogin(), u.getPassword(), "Administrateur");
} catch (Exception e) {
e.printStackTrace();
}
return state;
}
堆栈跟踪 :
Hibernate:选择 users0_.user_role_id_role 作为 user_ro10_1_0_,users0_.id_user 作为 id_user1_1_0_,users0_.id_user 作为 id_user1_1_1_,users0_.email 作为 email2_1_1_,users0_.is_deleted 作为 is_delet3_1_1_,users0_.login 作为 login4_1_1_,users_1_users_nom 作为密码 1_1_1_,users0_1_0 作为。 , users0_.prenom 作为 prenom7_1_1_, users0_.statut 作为 statut8_1_1_, users0_.telephone 作为 telephon9_1_1_, users0_.user_role_id_role 作为 user_ro10_1_1_ 来自用户 users0_ where users0_.user_role_id_role=? 2018-12-20 14:25:58.061 TRACE 6192 --- [nio-8080-exec-5] ohtype.descriptor.sql.BasicBinder:绑定参数1如 [BIGINT] - [3] 2018-12-20 14:26:36.015 WARN 6192 --- [nio-8080-exec-7] .wsmsDefaultHandlerExceptionResolver :由处理程序执行引起的已解决异常:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“POST”
java.lang.NullPointerException 在
com.akkaprofil.service.UserService.addUser(UserService.java:63) 在 com.akkaprofil.controller.UserControler.addUser(UserControler.java:46) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect。 NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod. java:205) 在 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) 的 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)在 org.springframework.web.servlet.mvc.method.annotation。RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:849) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:760) 在 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter。处理(AbstractHandlerMethodAdapter.java:85)在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) 在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) 在 org.springframework .web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) at javax.servlet.http.HttpServlet.service(HttpServlet.java:661 ) 在 org.springframework.web.servlet。FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache .catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain .java:193) 位于 org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) 的 org.springframework.web 的 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)。 filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.apache.catalina.core。ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) at org .springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain .java:166) 在 org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) 在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.apache.catalina。核。ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) at org .springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain .java:166) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 的 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)。身份验证器.AuthenticatorBase。调用(AuthenticatorBase.java:493)在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) 在 org.apache .catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) 在 org.apache.coyote.http11.Http11Processor.service(Http11Processor.java :800) 在 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806) 在 org.apache.tomcat.util.net 的 org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)。 NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) 在 org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 在 java.util。concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java .lang.Thread.run(未知来源)
当我调用 AddUser Request 方法时,触发了此异常,当找不到电子邮件地址时,我找不到未将值设置为用户对象的原因,所有类都用它们的 Stereotypes 进行注释,用户 Bean 是自动装配,我还没有声明一个新的操作员。
您能否澄清此异常背后的原因。谢谢
解决方案
推荐阅读
- gmail - 电子邮件模板中的转义字符
- python - 如何使用 AWS SessionToken 从 pyspark 中的 S3 读取?
- c# - 将后台工作人员传递给 VSTO Excel 插件中的另一个类
- r - 生成圆的点
- sql - 根据另一列的值排除从具有特定值的 case 语句派生的行
- jhipster - 在 JHipster 项目中手动添加实体
- typescript - Angular 5过滤器>地图>将图像分配给OpenWeather对象并在屏幕上显示不显示
- variables - 在范围循环中增加 struct var
- c - C - 关于字符串的预处理器声明
- enums - 如何在 Chapel 中声明 ENUM?