java - 无法使用休息服务

标签 java spring rest spring-boot microservices

我想创建两个通过休息调用进行通信的 spring-boot 项目。第一个包含带有 loginForm 的 UI 部分。第二个项目应该与数据库通信,并应该获取有关用户的信息,然后将其发送回 UI。

服务项目包含两个模块:data模块和impl模块。数据项目应包含 UI 和服务项目之间共享的公共(public)数据。它应该只是一个 jar,我将其添加为 UI 项目中的依赖项和服务项目中的 impl 模块。

impl 模块应包含实体、存储库、restcontrollers 和包含应用程序真实后端逻辑的服务层。我已经创建了 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);
}
}

它注入(inject) UserAuthenticationService,这是一个如下实现的接口(interface):

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);
}
}

当我运行它时,我得到:

*************************** APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in org.tu.userserviceimpl.service.impl.UserAuthenticationServiceImpl required a bean of type 'org.tu.userserviceimpl.repository.UserRepository' that could not be found.

我认为这很奇怪,因为 UserRepository 应该在那里可见,因为它是应用程序类下面的目录。为了解决这个问题,我在应用程序类上添加了 ComponentScan 注释:

@ComponentScan("org.tu.userserviceimpl.repository")

之后,项目构建和部署正常,但我无法访问它。我收到 404 错误。我什至在 UserController 中添加了这样的方法来解决问题:

@GetMapping("/hello")
public String hello() {
    return "hello";
}

但仍然无法访问。该应用程序部署在端口 8082 上,但是当我尝试访问“http://localhost:8082/user/hello”时却无法访问。之后,我尝试删除 UserAuthenticationServiceImpl 中注入(inject) UserRepository 的代码,并且还删除了 componentScan 注释。删除此代码后,我能够访问“http://localhost:8082/user/hello”,并在那里收到消息“hello”。这意味着问题出在存储库的某个地方。然后我尝试添加:

@EnableJpaRepositories("org.tu.userserviceimpl.repository")
@EntityScan("org.tu.userserviceimpl.entity") 

在应用程序类的顶部,我添加了再次将存储库注入(inject) 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

最佳答案

您已经在 UserAuthenticationServiceImpl 中创建了一个基于参数的构造函数。 如下:

public UserAuthenticationServiceImpl(UserRepository userRepository) {
    this.userRepository = userRepository;
}

因此JVM无法自动创建NO参数构造函数。
并且下面的行也没有 Autowiring

   private final UserRepository userRepository;

请添加无参数构造函数或删除现有的构造函数来解决此问题。

关于java - 无法使用休息服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57478585/

相关文章:

java - DispatcherServlet负责为Handler构造这个数据?

java - Spring、db和属性文件配置

ajax - Spring Boot - 外部 Tomcat 服务器 Ajax 身份验证失败消息

java - 防止 Jersey 客户端在发布大文件时导致内存不足错误

wcf - 单元测试 Web 服务的推荐模式

java - 将 Spring bean 注入(inject) RestEasy

java - Android 中异步抛出异常

node.js - 如何检查node.js和sqlite3更新是否成功?

java - 现有代码抛出此错误 [Hibernate + c3p0 + Oracle11g] : a resourcepool could not acquire a resource from its primary factory or source

java - 我可以确保我的 Spring ApplicationListener 之一最后执行吗?