应用程序在springboot未满足的依赖项中运行失败

kyxcudwk  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(320)

我是 Spring 开发的初学者。我试过使用SpringBoot2.4.1。激活我为该用户创建的激活链接,但它会导致以下错误:

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

这是我的代码:(存储库)

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

}

这是我的代码:(控制器)

@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";
    }

}
gk7wooem

gk7wooem1#

要使用SpringBoot,最好的方法是使用/导入SpringBootStarter依赖项,请参见pom.xml定义示例。

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

在您的示例中,我做了一些更改,如下所示:
通常域对象的名称是单数,请使用user而不是users

@Entity
public class User {

   @Id
   ...
   private Integer id;

   private String token;

   ...

   //Getters and Setters
}

更改存储库以删除@transactional并修复@param(较低)和查询错误。

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

}

添加服务实现

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

将控制器更改为使用服务

@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";
    }

}

运行应用程序

@SpringBootApplication
public class Application {

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

相关问题