简体   繁体   中英

Can't consume rest service

I want to create two spring-boot projects which communicate via rest calls. The first one contains the UI part with loginForm. The second project should communicate with the DB and should fetch the information about the user and then send it back to the 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. 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.

The impl module should contain entities, repositories, restcontrollers and a service layer containing the real back-end logic of the application. I have already created the UI project but I have problems with the service. In the data module I have created the classes with the user information in a package org.tu.userdata. In the service impl I have an userController like this:

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:

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.

I thought that's strange because the UserRepository should be visible there since its a directory below the application class. To solve this I added a ComponentScan annotation on the application class:

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

After that the project builds and deploys fine but I cannot access it. I got an 404 error. I even added an method like this one in the UserController just to troubleshoot it:

@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. After that I tried to remove the code in the UserAuthenticationServiceImpl which injects the UserRepository and I removed the componentScan annotation as well. After this code removal I was able to reach "http://localhost:8082/user/hello" and I got the message "hello" there. 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. 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. Any ideas? What I am doing wrong?

I uploaded almost the same project in github: https://github.com/lei-gustavson/user-service.git

You have already created an argument based constructor in UserAuthenticationServiceImpl . as below:

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

So NO argument constructor can't be created by JVM automatically.
and the below line has also not 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")

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.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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