提问人:huga721 提问时间:11/24/2022 更新时间:11/24/2022 访问量:483
Spring NullPointerException this.service 为 null
Spring NullPointerException this.service is null
问:
我的 NullPointerException 有问题,/register 端点上的每个 POST,NullPointerException 显示服务是带有 /register 的
null 控制器
@AllArgsConstructor
@RestController
@Validated
public class SecurityController {
private final UserService service;
@PostMapping("/register")
private ResponseEntity<UserDTO> registerUser (@RequestBody @Valid RegisterDTO registerDTO) {
return ResponseEntity.created(URI.create("/user")).body(service.createUser(registerDTO));
}
用户服务
@RequiredArgsConstructor
@Service
public class UserService implements UserServiceApi {
private final UserRepository userRepository;
private final RoleRepository roleRepository;
private final PasswordEncoder encoder;
@Override
public UserDTO createUser(RegisterDTO user) {
if (user.equals(userRepository.findByUsername(user.getUsername()))) {
throw new RuntimeException("This nickname is already taken");
}
if (user.equals(userRepository.findByEmail(user.getEmail()))) {
throw new RuntimeException("This email is already taken");
}
// Encoding password
user.setPassword(encoder.encode(user.getPassword()));
// On creating new Account it's going to have USER role
Role role = roleRepository.findByName("USER");
String username = user.getUsername();
String password = user.getPassword();
String email = user.getEmail();
User dto = buildUser(username, password, email, role);
userRepository.save(dto);
return UserDTO.builder()
.username(username)
.password(password)
.email(email)
.build();
}
其他使用服务的控制器,我不知道这可能是问题的线索
@RestController(value = "/user")
@AllArgsConstructor
@Validated
public class UserController {
private final UserService service;
@GetMapping("/getusers")
public ResponseEntity<List<User>> getAllUser() {
return ResponseEntity.ok(service.getUsers());
}
答: 暂无答案
评论
@Validated
public
@Validated
private
NullPointerException
public
@Validated
@AllArgsConstructor