如何将bean注入pojo?

wpcxdonn  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(265)

我的目标是注入 PasswordEncoderCreateUserModel 波乔。
我把 @Bean PasswordEncoder passwordEncoder 在配置类中。

@Configuration
public class Config {
   @Bean
   PasswordEncoder getPasswordEncoder() {
     return new BCryptPasswordEncoder(10);
   }
}

我希望我的pojo看起来像这样:

public class CreateUserModel {
  private String username;
  private String password;
  private String name;
  private String role;

  private final PasswordEncoder passwordEncoder;

  public CreateUserModel(PasswordEncoder passwordEncoder, String username, String password, String name, String role) {
    this.passwordEncoder = passwordEncoder;
    this.username = username;
    this.password = passwordEncoder.encode(password);
    this.name = name;
    this.role = role;
  }

  public String getUsername() {
    return username;
  }

  public String getPassword() {
    return password;
  }

  public String getName() {
    return name;
  }

  public String getRole() {
    return role;
  }
}

这就是我如何使用 CreateUserModel 波乔。

@RestController
public class OrganizationController {
  @PostMapping("/organization/createuser")
  public CreateUserModel createUser(@RequestBody CreateUserModel user) {
    return user;
  }
}

用户将发送带有正文的post请求:

{
  "username": "user",
  "passsword": "secret",
  "name": "user client",
  "role": "1"
}

我的期望结果发送邮寄请求到 /organization/createuser 是:

{
  "username": "user",
  "passsword": "alv4ko023j4v2lkralfj",
  "name": "user client",
  "role": "1"
}

我的实际结果是,服务器抛出

2021-02-20 21:32:26.614  WARN 202592 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.example.satpamspringboot.model.organization.CreateUserModel`, problem: `java.lang.NullPointerException`; nested exception is com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `com.example.satpamspringboot.model.organization.CreateUserModel`, problem: `java.lang.NullPointerException`
 at [Source: (PushbackInputStream); line: 6, column: 1]]

我能想到的唯一解决方案是使用dto模式,它需要5个单独的文件:
带有@restcontroller、@postmapping、@requestbody的文件
dto对象的文件。
使用@service存档
使用@mapper文件
dao对象的文件。
通过注入 PasswordEncoderCreateUserModel 波乔,这会减少样板:
结合dto和dao对象。
用@service删除文件。

enyaitl3

enyaitl31#

把进程移到你的控制器里?

@RestController
public class OrganizationController {

@Autowired
PasswordEncoder passwordEncoder;

  @PostMapping("/organization/createuser")
  public CreateUserModel createUser(@RequestBody CreateUserModel user) {
    String password = passwordEncoder.encode(user.getPassword());
    return new  CreateUserModel(user.getUsername(), password, user.getName(), user.getRole());
  }
}

然后也更新你的pojo

public class CreateUserModel {
  private String username;
  private String password;
  private String name;
  private String role;

  public CreateUserModel(String username, String password, String name, String role) {
    this.username = username;
    this.password = password
    this.name = name;
    this.role = role;
  }

  public String getUsername() {
    return username;
  }

  public String getPassword() {
    return password;
  }

  public String getName() {
    return name;
  }

  public String getRole() {
    return role;
  }
}

通过将passwordencoder注入createusermodel pojo,它将减少样板文件:
结合dto和dao对象。用@service删除文件。
到现在为止,你已经达到了你想要的,记住这不是最佳实践。

相关问题