Java Can';不要使用休息服务
我想创建两个spring引导项目,它们通过rest调用进行通信。第一个包含带有loginForm的UI部分。第二个项目应与数据库通信,并应获取有关用户的信息,然后将其发送回UI 服务项目包含两个模块:数据模块和impl模块。数据项目应该包含UI和服务项目之间共享的公共数据。它应该只是一个jar,我将它作为依赖项添加到UI项目中,并作为impl模块添加到服务项目中 impl模块应该包含实体、存储库、RESTController和一个包含应用程序真正后端逻辑的服务层。我已经创建了UI项目,但是服务有问题。在数据模块中,我在org.tu.userdata包中创建了包含用户信息的类。在service impl中,我有一个userController,如下所示:Java Can';不要使用休息服务,java,spring,rest,spring-boot,microservices,Java,Spring,Rest,Spring Boot,Microservices,我想创建两个spring引导项目,它们通过rest调用进行通信。第一个包含带有loginForm的UI部分。第二个项目应与数据库通信,并应获取有关用户的信息,然后将其发送回UI 服务项目包含两个模块:数据模块和impl模块。数据项目应该包含UI和服务项目之间共享的公共数据。它应该只是一个jar,我将它作为依赖项添加到UI项目中,并作为impl模块添加到服务项目中 impl模块应该包含实体、存储库、RESTController和一个包含应用程序真正后端逻辑的服务层。我已经创建了UI项目,但是服务有
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);
}
}
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();
}
}
它注入UserAuthenticationService,这是一个实现如下的接口:
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);
}
}
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);
}
当我运行它时,我得到:
***************************应用程序无法启动
说明: 中构造函数的参数0 org.tu.userserviceimpl.service.impl.UserAuthenticationServiceImpl 需要类型为的bean “org.tu.userserviceimpl.repository.UserRepository”无法删除 找到了 我认为这很奇怪,因为UserRepository应该在那里可见,因为它是应用程序类下面的一个目录。为了解决这个问题,我在应用程序类上添加了ComponentScan注释:
package org.tu.userserviceimpl;
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
@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我收到了“你好”的信息。这意味着问题在存储库的某个地方。然后我试图补充:
@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中上传了几乎相同的项目:您已经在
UserAuthenticationServiceImpl
中创建了一个基于参数的构造函数。
详情如下:
public UserAuthenticationServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
因此,JVM不能自动创建任何参数构造函数。
下面的一行也没有自动连线
private final UserRepository userRepository;
请不要添加参数构造函数或删除现有的构造函数以解决此问题。您已经在
UserAuthenticationServiceImpl
中创建了基于参数的构造函数。
详情如下:
public UserAuthenticationServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
因此,JVM不能自动创建任何参数构造函数。
下面的一行也没有自动连线
private final UserRepository userRepository;
请不要添加参数构造函数或删除现有的构造函数来解决此问题。您可以尝试将下面的语句放在下面吗 @实体扫描(“组织”) 我也遇到过同样的问题,但当我将上面的语句用于我的目录结构时,它就起作用了 注: 您混淆了@ComponentScan和@EntityScan 只需做一件事,上面两项都要做
@ComponentScan("org"),
@EntityScan("org") and
@EnableJPARepository("org")
大多数情况下,这对你来说是可行的。你能试着把下面的陈述写下来吗 @实体扫描(“组织”) 我也遇到过同样的问题,但当我将上面的语句用于我的目录结构时,它就起作用了 注: 您混淆了@ComponentScan和@EntityScan 只需做一件事,上面两项都要做
@ComponentScan("org"),
@EntityScan("org") and
@EnableJPARepository("org")
基本上它会对你有用的谢谢大家帮助我!我找到了解决办法。问题是我没有JPA文件。这就解决了问题:
@Configuration
@EnableJpaRepositories("org.tu.userserviceimpl.repository")
public class JPAConfig {
}
之后,我从SpringBootApplication类中删除了ComponentScan注释,所有内容都已启动并运行谢谢大家的帮助!我找到了解决办法。问题是我没有JPA文件。这就解决了问题:
@Configuration
@EnableJpaRepositories("org.tu.userserviceimpl.repository")
public class JPAConfig {
}
之后,我从SpringBootApplication类中删除了ComponentScan注释,所有内容都已启动并运行Hi,@ThePrinter!谢谢你的建议。因此,根据您所说的,我向服务添加了一个无参数构造函数,并尝试在应用程序类上没有ComponentScan的情况下再次部署它。它给出了同样的错误。所以我又添加了组件扫描。它部署得很好,但我仍然无法联系到服务。我试图在上再次访问它,但得到了相同的白色标签错误页面。此外,我还尝试向UserController添加一个无参数构造函数,但结果是相同的。我错过什么了吗?如果你愿意,你可以签出这个项目。我在github上上传了几乎相同的项目:github.com/lei-gustavson/user-service.git。谢谢大家!@Lei当您获得“白标签错误页”时,您可以共享错误日志吗!谢谢你的建议。因此,根据您所说的,我向服务添加了一个无参数构造函数,并尝试在应用程序类上没有ComponentScan的情况下再次部署它。它给出了同样的错误。所以我又添加了组件扫描。它部署得很好,但我仍然无法联系到服务。我试图在上再次访问它,但得到了相同的白色标签错误页面。此外,我还尝试向UserController添加一个无参数构造函数,但结果是相同的。我错过什么了吗?如果你愿意,你可以签出这个项目。我在github上上传了几乎相同的项目:github.com/lei-gustavson/user-service.git