简体   繁体   中英

How to inject a bean into a POJO?

My goal is to inject PasswordEncoder to a CreateUserModel POJO.

I put the @Bean PasswordEncoder passwordEncoder in a config class.

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

This is how I wish my POJO looks like:

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

This is how I would use the CreateUserModel POJO.

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

A user will send a POST request with a body:

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

My expected result for sending POST request to /organization/createuser is:

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

My actual result, the server throws

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

The only solution I can think of is to use the DTO pattern, which require 5 separate files:

  1. File with @RestController, @PostMapping, @RequestBody
  2. File for the DTO object.
  3. File with @Service
  4. File with @Mapper
  5. File for the DAO object.

By injecting PasswordEncoder to the CreateUserModel POJO, it will reduce the boilerplate:

  1. Combine the DTO and DAO object.
  2. Remove File with @Service.

Just move the process in your controller?

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

Then also update your 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;
  }
}

By injecting PasswordEncoder to the CreateUserModel POJO, it will reduce the boilerplate:

Combine the DTO and DAO object. Remove File with @Service.

By now you would have achieved what you wanted, keep in mind however that this is not considered best practice.

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