【问题标题】:Can't consume rest service无法消费休息服务
【发布时间】: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


【解决方案1】:

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

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

所以 JVM 不能自动创建 NO 参数构造函数。
并且下面的行也没有autowired

   private final UserRepository userRepository;

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

【讨论】:

  • 嗨,@TheSprinter!感谢您的建议。因此,根据您所说,我向服务添加了一个无参数构造函数,并且我尝试在没有应用程序类上的 ComponentScan 的情况下再次部署它。它给出了同样的错误。所以我再次添加了 ComponentScan。它部署得很好,但我仍然无法访问该服务。我试图在localhost:8082/user/hello 上再次访问它,我得到了相同的 Whitelabel 错误页面。此外,我还尝试向 UserController 添加一个无参数构造函数,但结果是一样的。我错过了什么吗?
  • 您可以根据需要签出项目。我在 github 上上传了几乎相同的项目:github.com/lei-gustavson/user-service.git。谢谢!
  • @Lei 你能在获得“Whitelabel Error Page”时分享错误日志
【解决方案2】:

你能试试下面的语句吗?

@EntityScan("org")

我也遇到过同样的问题,但是当我将上述语句用于我的目录结构时,它可以工作。

注意: 您对@ComponentScan 和@EntityScan 感到困惑。

只需在上面的注解注释上做一件事,

@ComponentScan("org"), 
@EntityScan("org") and 
@EnableJPARepository("org")

大多数情况下它会为你工作

【讨论】:

  • 嗨,@vishal laknyani!我试过你的建议,但没有成功。我只在 org 上添加了 EntityScan,首先它给了我: org.tu.userserviceimpl.service.impl.UserAuthenticationServiceImpl 中的字段 userRepository 需要一个找不到的 'org.tu.userserviceimpl.repository.UserRepository' 类型的 bean。然后我在 org 上添加了带有 org 和 EntityScan 的 ComponentScan,我得到了“@ComponentScan("org")”。之后,我保留了 EntityScan 并将 ComponentScan 编辑为:“@ComponentScan("org.tu.userserviceimpl.repository")”,但每次尝试访问该服务时仍然得到 404。谢谢!
  • 您可以根据需要签出项目。我在 github 上上传了几乎相同的项目:github.com/lei-gustavson/user-service.git。谢谢!
【解决方案3】:

感谢大家帮助我!我找到了解决办法。问题是我没有 JPAConfig 文件。这解决了问题:

@Configuration
@EnableJpaRepositories("org.tu.userserviceimpl.repository")
public class JPAConfig {
}

之后,我从 SpringBootApplication 类中删除了 ComponentScan 注释,一切正常运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2019-05-25
    • 1970-01-01
    • 2012-03-08
    • 2015-11-25
    相关资源
    最近更新 更多