我正在创建注册表,但是在 Itelij 中名为 roles.sql 的文件中的表中插入多行时遇到问题。我选择了PostgreSQL作为数据库。 乍一看,查询看起来不错,我不知道问题出在哪里。
INSERT INTO public.roles (name) VALUES ('ROLE_USER'), ('ROLE_ADMIN');
我已经在名为 application.properties
的文件中配置了该程序spring.datasource.url= jdbc:postgresql://localhost:5432/testdb?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
logging.level.org.hibernate.SQL= DEBUG
## App Properties
app.jwtSecret= JWTSuperSecretKey
app.jwtExpirationInMs = 604800000
## Jackson Properties
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS= false
spring.jackson.time-zone= UTC
这是在名为AuthController的类中进行注册的代码
// Creating user's account
User user = new User(signUpRequest.getName(), signUpRequest.getUsername(),
signUpRequest.getEmail(), signUpRequest.getPassword());
user.setPassword(passwordEncoder.encode(user.getPassword()));
Role userRole = roleRepository.findByName(RoleName.ROLE_USER)
.orElseThrow(() -> new AppException("User Role not set."));
user.setRoles(Collections.singleton(userRole));
User result = userRepository.save(user);
URI location = ServletUriComponentsBuilder
.fromCurrentContextPath().path("/api/users/{username}")
.buildAndExpand(result.getUsername()).toUri();
return ResponseEntity.created(location).body(new ApiResponse(true, "User registered successfully"));
我尝试使用 Postman 注册用户,并在下面的图片中给出响应
{
"timestamp": "2018-11-26T14:10:20.833+0000",
"status": 500,
"error": "Internal Server Error",
"message": "User Role not set.",
"path": "/api/auth/signup"
}
请有人可以向我解释未在数据库中插入行的问题吗?预先谢谢你!