-1

I am a beginner in spring development. I tried using Spring boot 2.4.1. Activate the activation link I created for the user, but it gives me the following error:

Error starting ApplicationContext.To display the conditions report re-run your application with 'debug' enabled.
2021-01-10 00:07:22.711 ERROR 9560 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject

Here is my code :(Repository)

@Repository
public interface UsersRepositor extends JpaRepository<Users,Integer> {
    public Users findByEmail(String email);
    public Users findByToken(String token);
    @Query("update  Users U set U.active=true where U.token =:token and u.id:id")
    @Modifying
    @Transactional
    public void activeUsersByTokenAndId(@Param("token")String token,@param("id")int id);

}

Here is my code :(Controller)

@Controller

public class ActivationController {
   @Autowired
    UsersRepositor usersRepositor;

@GetMapping(value = "/activation/{token}")

        public String activeUsersByToken(
            @PathVariable("token")String token
    ){
        Users users=usersRepositor.findByToken(token);
        if (users !=null){
            usersRepositor.activeUsersByTokenAndId(token,users.getId());
        }

        return "redirect:/login";
    }

}

Mina
  • 48
  • 7

2 Answers2

0

To use Spring Boot the best way is to use/import spring boot starter dependencies, see pom.xml definition example.

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

In your example I made some changes, see below:

Normally the name of an domain object is singular, use User instead Users

@Entity
public class User {

   @Id
   ...
   private Integer id;

   private String token;

   ...
   
   //Getters and Setters
}

Change repository to remove @Transactional and fix @param (lower) and query errors.

@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
    public User findByEmail(String email);
    public User findByToken(String token);
    
    @Query("update  User u set u.active = true where u.token = :token and u.id = :id")
    @Modifying
    public void activeUserByTokenAndId(@Param("token")String token, @Param("id")int id);

}

Add service implementation

@Service
public class UserService {

   private final UserRepository userRepository;

   public UserService(final UserRepository userRepository){
      this.userRepository = userRepository;
   }

   @Transactional
   public void activeUser(final String token){
      User user= userRepository.findByToken(token);
      if (users ==null){
         // TODO throw exception like
         // throw new UserNotFoundException();
      }
      this.userRepository.activeUserByTokenAndId(token, id);
   }
}

Change controller to use service

@RestController
public class ActivationController {
   
   private final UserService userService;

   public ActivationController (final UserService userService){
      this.userService = userService;
   }

   @GetMapping(value = "/activation/{token}")
   public String activeUsersByToken(@PathVariable("token")String token){
        userService.activeUser(token);
        
        return "redirect:/login";
    }

}

Run application

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Luis Costa
  • 1,150
  • 7
  • 8
  • Unfortunately,I did, but it still have error `Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'activationController' defined in file [F:\auto\target\classes\project\bigner\automation\controller\ActivationController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating` – Mina Jan 09 '21 at 22:57
  • if you copy my implementation, you should not have any problem. but for the error that um past here, your controller can not find UserService bean. To solve this you need to 1) put UserService in same package that "Application" class (start name needs to be same) for example if you have "com.project.Application" your service needs to stay in "com.project" package (example: "com.project.core.user.UserService") OR 2) use "@ComponentScan(basePackages = "com.project")" to scan your packages. – Luis Costa Jan 09 '21 at 23:29
0
public class ActivationController {

    UsersRepositor usersRepositor

    @Autowired
    ActivationController(UsersRepositor usersRepositor) {
  
    this.usersRepositor = usersRepositor;
}

instead you try constructor injection

kohane15
  • 809
  • 12
  • 16
srikanth
  • 61
  • 3