简体   繁体   English

不能消费休息服务

[英]Can't consume rest service

I want to create two spring-boot projects which communicate via rest calls.我想创建两个通过休息调用进行通信的 spring-boot 项目。 The first one contains the UI part with loginForm.第一个包含带有 loginForm 的 UI 部分。 The second project should communicate with the DB and should fetch the information about the user and then send it back to the UI.第二个项目应该与 DB 通信,并应该获取有关用户的信息,然后将其发送回 UI。

The service project contains two modules: data module and impl module.服务项目包含两个模块:数据模块和实现模块。 The data project should contain the common data shared between the UI and the service project.数据项目应包含 UI 和服务项目之间共享的公共数据。 It should be only a jar which I will add as a dependency in the UI project and the impl module in the service project.它应该只是一个 jar,我将在 UI 项目和服务项目中的 impl 模块中添加它作为依赖项。

The impl module should contain entities, repositories, restcontrollers and a service layer containing the real back-end logic of the application. impl 模块应该包含实体、存储库、restcontroller 和一个包含应用程序真实后端逻辑的服务层。 I have already created the UI project but I have problems with the service.我已经创建了 UI 项目,但是我遇到了服务问题。 In the data module I have created the classes with the user information in a package org.tu.userdata.在数据模块中,我使用包 org.tu.userdata 中的用户信息创建了类。 In the service impl I have an userController like this:在服务实现中,我有一个像这样的 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);
}
}

It injects the UserAuthenticationService which is an interface implemented like this:它注入 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();
}
}

the UserRepository:用户存储库:

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

and an application class:和一个应用程序类:

package org.tu.userserviceimpl;

@SpringBootApplication
public class UserServiceApplication {

public static void main(String[] args) {
    SpringApplication.run(UserServiceApplication.class, args);
}
}

When I run it I get:当我运行它时,我得到:

*************************** 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. org.tu.userserviceimpl.service.impl.UserAuthenticationServiceImpl 中构造函数的参数 0 需要一个无法找到的类型为“org.tu.userserviceimpl.repository.UserRepository”的 bean。

I thought that's strange because the UserRepository should be visible there since its a directory below the application class.我认为这很奇怪,因为 UserRepository 应该在那里可见,因为它是应用程序类下面的一个目录。 To solve this I added a ComponentScan annotation on the application class:为了解决这个问题,我在应用程序类上添加了一个 ComponentScan 注释:

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

After that the project builds and deploys fine but I cannot access it.之后,项目构建和部署正常,但我无法访问它。 I got an 404 error.我收到 404 错误。 I even added an method like this one in the UserController just to troubleshoot it:我什至在 UserController 中添加了一个这样的方法来解决它:

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

But still cannot access it.但仍然无法访问它。 The application is deployed on port 8082 but when I try to access "http://localhost:8082/user/hello" I cannot.该应用程序部署在端口 8082 上,但是当我尝试访问“http://localhost:8082/user/hello”时却无法访问。 After that I tried to remove the code in the UserAuthenticationServiceImpl which injects the UserRepository and I removed the componentScan annotation as well.之后,我尝试删除注入 UserRepository 的 UserAuthenticationServiceImpl 中的代码,并且我也删除了 componentScan 注释。 After this code removal I was able to reach "http://localhost:8082/user/hello" and I got the message "hello" there.删除此代码后,我能够访问“http://localhost:8082/user/hello”,并在那里收到消息“hello”。 This means that the problem is somewhere in the Repository.这意味着问题出在存储库中的某个地方。 Then I tried to add:然后我尝试添加:

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

on top of the application class and I added the code which injects the repository in the UserAuthenticationServiceImpl again.在应用程序类的顶部,我添加了再次将存储库注入 UserAuthenticationServiceImpl 的代码。 This time the outcome was a different error:这次的结果是不同的错误:

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

I deleted the EntityScan annotation but the result was the same.我删除了 EntityScan 注释,但结果是一样的。 Any ideas?有任何想法吗? What I am doing wrong?我做错了什么?

I uploaded almost the same project in github: https://github.com/lei-gustavson/user-service.git我在github上传了几乎相同的项目: https : //github.com/lei-gustavson/user-service.git

You have already created an argument based constructor in UserAuthenticationServiceImpl .您已经在UserAuthenticationServiceImpl创建了一个基于参数的构造函数。 as below:如下:

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

So NO argument constructor can't be created by JVM automatically.因此, JVM无法自动创建 NO 参数构造函数。
and the below line has also not autowired并且下面的行也没有autowired

   private final UserRepository userRepository;

Please add no argument constructor or remove the existing to resolve this issue.请添加无参数构造函数或删除现有的构造函数以解决此问题。

Can you try putting below statement ?你可以试着把下面的声明?

@EntityScan("org") @EntityScan("组织")

I also have faced same issue but when i have used above statement for my directory structure it works.我也遇到了同样的问题,但是当我将上述语句用于我的目录结构时,它可以工作。

Note : You are confused between @ComponentScan and @EntityScan.注意:您对@ComponentScan 和@EntityScan 感到困惑。

Just do one thing both above annotation annote like,只需在注释注释上方做一件事,例如,

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

Mostly it will work for you大多数情况下它会为你工作

Thank you all for trying to help me!感谢大家试图帮助我! I find a solution.我找到了解决办法。 The problem was that i didnt have JPAConfig file.问题是我没有 JPAConfig 文件。 This solved the problem:这解决了这个问题:

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

After that i deleted the ComponentScan annotation from the SpringBootApplication class and everything was up and running之后,我从 SpringBootApplication 类中删除了 ComponentScan 注释,一切都启动并运行了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM