【发布时间】:2021-06-10 06:10:58
【问题描述】:
我想创建两个通过 rest 调用进行通信的 spring-boot 项目。第一个包含带有 loginForm 的 UI 部分。第二个项目应与 DB 通信,并应获取有关用户的信息,然后将其发送回 UI。
服务项目包含两个模块:数据模块和实现模块。数据项目应包含 UI 和服务项目之间共享的公共数据。它应该只是一个 jar,我将在 UI 项目中作为依赖项添加,在服务项目中添加 impl 模块。
impl 模块应该包含实体、存储库、restcontroller 和一个包含应用程序真正后端逻辑的服务层。我已经创建了 UI 项目,但服务有问题。在数据模块中,我在包 org.tu.userdata 中创建了包含用户信息的类。在服务实现中,我有一个这样的 userController:
package org.tu.userserviceimpl.controller;
@RestController
@RequestMapping("/user")
public class UserController {
private final UserAuthenticationService userService;
@Autowired
public UserController(UserAuthenticationService userService) {
this.userService = userService;
}
@PostMapping(value = { "/logUser" })
public UserDto logUser(@RequestBody AuthenticateUserDto authenticateUserDto) throws Exception {
return userService.logUser(authenticateUserDto);
}
@PostMapping(value = { "/register" })
public UserDto register(@RequestBody RegisterUserDto registerUserDto) throws Exception {
return userService.registerUser(registerUserDto);
}
}
它注入 UserAuthenticationService ,这是一个像这样实现的接口:
package org.tu.userserviceimpl.service.impl;
@Service
public class UserAuthenticationServiceImpl implements UserAuthenticationService {
private final UserRepository userRepository;
@Autowired
public UserAuthenticationServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public UserDto registerUser(RegisterUserDto registerUserDto) throws AuthenticationException {
return new UserDto();
}
@Override
public UserDto logUser(AuthenticateUserDto authenticateUserDto)
throws UserPrincipalNotFoundException, AuthenticationException {
return new UserDto();
}
}
用户存储库:
package org.tu.userserviceimpl.repository;
@Repository
public interface UserRepository extends JpaRepository<UserEntity, Long> {
UserEntity findByUsername(String username);
boolean existsByUsername(String username);
boolean existsByEmail(String email);
}
和一个应用程序类:
package org.tu.userserviceimpl;
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
当我运行它时,我得到:
***************************应用程序启动失败
说明:
构造函数的参数0 org.tu.userserviceimpl.service.impl.UserAuthenticationServiceImpl 需要一个 bean 类型 'org.tu.userserviceimpl.repository.UserRepository' 不可能 找到了。
我认为这很奇怪,因为 UserRepository 应该在那里可见,因为它是应用程序类下方的目录。为了解决这个问题,我在应用程序类上添加了一个 ComponentScan 注释:
@ComponentScan("org.tu.userserviceimpl.repository")
之后项目构建和部署正常,但我无法访问它。我收到 404 错误。我什至在 UserController 中添加了一个类似的方法来解决它:
@GetMapping("/hello")
public String hello() {
return "hello";
}
但仍然无法访问它。该应用程序部署在端口 8082 上,但是当我尝试访问“http://localhost:8082/user/hello”时,我无法访问。之后,我尝试删除注入 UserRepository 的 UserAuthenticationServiceImpl 中的代码,并且还删除了 componentScan 注释。删除此代码后,我能够访问“http://localhost:8082/user/hello”,并在那里收到消息“hello”。这意味着问题出在存储库中。然后我尝试添加:
@EnableJpaRepositories("org.tu.userserviceimpl.repository")
@EntityScan("org.tu.userserviceimpl.entity")
在应用程序类的顶部,我添加了再次在 UserAuthenticationServiceImpl 中注入存储库的代码。这次结果是另一个错误:
Parameter 0 of constructor in org.tu.userserviceimpl.service.impl.UserAuthenticationServiceImpl required a bean named 'entityManagerFactory' that could not be found.
我删除了 EntityScan 注释,但结果是一样的。有任何想法吗?我做错了什么?
我在github上传了几乎相同的项目:https://github.com/lei-gustavson/user-service.git
【问题讨论】:
-
为什么要实现
UserRepository?您还可以添加完整的错误日志而不是简单地“应用程序启动失败” -
嗨。 @ArnaudClaudel。自从它的 JPARepository 以来,我还没有实现 UserRepository。日志是:应用程序启动失败描述:org.tu.userserviceimpl.service.impl.UserAuthenticationServiceImpl 中构造函数的参数 0 需要找不到类型为“org.tu.userserviceimpl.repository.UserRepository”的 bean。很抱歉错误消息的格式不正确,感谢您的帮助!
-
确实我误读了
UserAuthenticationServiceImpl,我认为这是UserRepository的实现。 -
没问题。如果需要,您可以签出该项目。我在 github 上上传了几乎相同的项目:github.com/lei-gustavson/user-service.git。谢谢!
标签: java spring rest spring-boot microservices