当前位置: 首页 > 知识库问答 >
问题:

我的项目Spring Boot在创建Bean中出现的问题

魏旭
2023-03-14

我的UserController

@RestController
@RequestMapping(path= "/api")
@RequiredArgcContructor
public class UserController {

    
    private final UserService userService;

    @PostMapping
    public String create(@RequestBody UserDTO userDto){
        return  userService.save(userDto);
    }

}

我的用户服务

@Service
@RequiredArgsConstructor
public class UserService {

 
    private  UserManager userManager;
    private  Mapper<UserDTO, UserEntity> userDTOUserMapper;

    public String save(UserDTO userDto) {
        return userManager.signUp(userDTOUserMapper.map(userDto));
    }

}

我的用户管理器

   package com.userlogintask.userlogintask.business;

import com.userlogintask.userlogintask.model.TokenEntity;
import com.userlogintask.userlogintask.model.UserEntity;
import com.userlogintask.userlogintask.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.UUID;

@Component
@RequiredArgsConstructor
public class UserManager implements UserDetailsService {

    private final static String USER_NOT_FOUND_msg="user with email %s not found";
   
    private final UserRepository userRepository;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    private final TokenManager tokenManager;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        return userRepository.findByEmail(email)
                .orElseThrow(()-> new UsernameNotFoundException(String.format(USER_NOT_FOUND_msg,email)));
    }


    public String signUp(UserEntity userEntity){
        boolean isExist =  userRepository.findByEmail(userEntity.getEmail()).isPresent();

        if(isExist){
            throw new IllegalStateException("email is already exists");
        }
        String encodePassword = bCryptPasswordEncoder.encode(userEntity.getPassword());
        userEntity.setPassword(encodePassword);
        userRepository.save(userEntity);

        //TODO:Send confirmation token;
        String token = UUID.randomUUID().toString();

        TokenEntity confirmationTokenEntity = new TokenEntity(
                token,
                LocalDateTime.now(),
                LocalDateTime.now().plusMinutes(15),
                userEntity
        );
        tokenManager.saveConfirmationToken(confirmationTokenEntity);

        //TODO:Send email

        return  token;
    }

    public int enableAppUser(String email) {
        return userRepository.enableUser(email);
    }

}
 @Configuration
public class ApiMappingConfig {

    //userDTO mapping user

    @Bean
    public Mapper<UserDTO, UserEntity> userDTOUserMapper(){
        return Mapping.from(UserDTO.class)
                .to(UserEntity.class)
                .omitInDestination(UserEntity::getLocked)
                .omitInDestination(UserEntity::getUser_id)
                .omitInDestination(UserEntity::getEnabled)
                .mapper();
    }
}
    
 @Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class UserEntity implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id",unique = true)
    private Long user_id;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    private String email;
    private String password;
    @Enumerated(EnumType.STRING)
    private UserRole userRole;

    private Boolean locked = false;
    private Boolean enabled = false;

    public UserEntity(String firstName,
                      String lastName, String email,
                      String password, UserRole userRole) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.userRole = userRole;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.singleton(new SimpleGrantedAuthority(userRole.name()));
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return email;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

}
  @Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDTO {

    private String firstName;
    private String lastName;
    private String email;
    private String password;
    private UserRole userRole;

}
 @SpringBootApplication
public class UserlogintaskApplication {

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

}

我的错误

我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.userlogintask</groupId>
    <artifactId>userlogintask</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>userlogintask</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>15</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.20</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.remondis</groupId>
            <artifactId>remap</artifactId>
            <version>4.2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>4.3.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.4</version>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
        

问题出在哪里?我不明白

共有1个答案

单修德
2023-03-14

好的,看起来错误是:

.MappingException: The get-method for property 'enabled' in type com.userlogintask.userlogintask.model.UserEntity is not a valid Java Bean property. at

这就是为什么映射器不能按需要创建的原因,这又会导致服务的BeanInitializationException

查看您的实体,您使用:boolean表示enabled属性,但是getter返回boolean(不是包装类,而是一个真正的原语)。试着改变它,看看会发生什么。

 类似资料:
  • 我正在学习使用laravel创建项目,我正在使用PHP8。通过“laravel新建项目”创建新项目时。我得到了这个错误,供应商文件夹在该项目中丢失。

  • 问题内容: 早在12月,我就从Eclipse切换到了IntelliJ IDEA,但几天后又切换回了。今天,我再次尝试了IntelliJ,基本上只剩下一期了。 问题是我想在保存时编译Java类。原因是我正在使用JRebel,它将选择新类。我不想每次都按+ 来编译更改的类。 我阅读了有关EclipseModel插件的信息,但似乎不再起作用了,可以在该插件的页面上的注释中看到:http : //plug

  • 我转到New Project>Maven>Web Application,尝试创建一个Maven项目。我使用的是glassfish服务器4.1.1和java EE 7 Web。当我点击finish时,我得到了这个错误。 要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven。使用-x开关重新运行Maven以启用完全调试日志记录。 有关错误和可能的解决方案的详细信息,请阅读以下文章:[帮助1]h

  • 当我想要在中创建一个新的项目时,当我进入命名项目的阶段时,我会收到以下错误消息: "无法自动设置JavaFX平台。请转到Platform Manager,创建一个非默认JavaSE平台,然后转到JavaFX选项卡,启用JavaFX并填写有效JavaFX SDK和JavaFX运行时的路径。注意:JavaFX SDK可从JavaFX网站下载” 我如何解决这个问题?

  • 我试图在我的Micronaut应用程序中使用jOOQ,并将jOOQ DSLContext作为一个注入bean自动提供给我的构造函数,但它无法找到该bean。 我在application.yml中配置了数据源以连接到postgres db,并声明了如下构造函数: 我在我的构建中包含了以下依赖项。gradle 我希望可以访问DSLContext并在RepositoryImpl类中编写查询,但在尝试使用