简体   繁体   中英

Spring data cassandra: No property builder found on class User to bind constructor to

I started a basic Spring project. I use Spring data Cassandra.

My model use the builder pattern. Here's my User class:

@Table("user")
@JsonDeserialize(builder = User.Builder.class)
public class User {

@PrimaryKey
public final String username;
@Column("password")
public final String password;
@Column("firstname")
public final String firstName;
@Column("lastname")
public final String lastName;
@Column("birthdate")
public final Date birthDay;
@Column("emailaddress")
public final String emailAddress;
@Column("organization")
public final String organization;
@Column("locality")
public final String locality;
@Column("stateprovince")
public final String stateProvince;
@Column("countrycode")
public final String countryCode;
@Column("disabled")
public final boolean disabled;
@Column("registrationdate")
public final Date registrationDate;

private User(Builder builder) {
    username = builder.username;
    password = builder.password;
    firstName = builder.firstName;
    lastName = builder.lastName;
    birthDay = builder.birthDay;
    emailAddress = builder.emailAddress;
    organization = builder.organization;
    locality = builder.locality;
    stateProvince = builder.stateProvince;
    countryCode = builder.countryCode;
    disabled = builder.disabled;
    registrationDate = builder.registrationDate;
}

public static Builder newBuilder() {
    return new Builder();
}

public static Builder newBuilder(User copy) {
    Builder builder = new Builder();
    builder.username = copy.username;
    builder.password = copy.password;
    builder.firstName = copy.firstName;
    builder.lastName = copy.lastName;
    builder.birthDay = copy.birthDay;
    builder.emailAddress = copy.emailAddress;
    builder.organization = copy.organization;
    builder.locality = copy.locality;
    builder.stateProvince = copy.stateProvince;
    builder.countryCode = copy.countryCode;
    builder.disabled = copy.disabled;
    builder.registrationDate = copy.registrationDate;
    return builder;
}

public static final class Builder {
    private String username;
    private String password;
    private String firstName;
    private String lastName;
    private Date birthDay;
    private String emailAddress;
    private String organization;
    private String locality;
    private String stateProvince;
    private String countryCode;
    private boolean disabled;
    private Date registrationDate;

    private Builder() {
    }

    public static Builder newStub() {
        return new Builder()
                .withUsername("stub")
                .withEmailAddress("stub@unittest.com")
                .withPassword("dontforgettests")
                .withCountryCode("9")
                .withFirstName("Someone")
                .withLastName("Overarainbow");
    }

    public Builder withUsername(String val) {
        username = val;
        return this;
    }
     //etc
}

I have no problem on the controller side to serialize the json from a POST request to User object. There's also no problem when I call the repo to save the object, but when I do :

User getByUsername(String username);

Then I get the following error: No property builder found on entity class com.project.model.user.User to bind constructor parameter

How can I solve this issue?

To create Spring Data entities, there are mandatory elements that should be written in your POJO, you should have full arguments constructor, getters and setters. Also, don't hesitate to add the default constructor and to override toString with equals and hashCode methods. This is an example of an Entity class using Spring Data Cassandra terminology:

@Table
public class Person {

  @PrimaryKey
  private final String id;

  private final String name;
  private final int age;

  public Person(String id, String name, int age) {
    this.id = id;
    this.name = name;
    this.age = age;
  }

  public String getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public int getAge() {
    return age;
  }

  @Override
  public String toString() {
    return String.format("{ @type = %1$s, id = %2$s, name = %3$s, age = %4$d }",
      getClass().getName(), getId(), getName(), getAge());
  }
}

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