Search code examples
javadesign-patternsjacksonbuilder

Builder pattern deserialize with jackson not working


I try to handle request

{
  "city": "city",
  "date": "2020-08-26T12:15:54.428Z",
  "time": {
      "sum":"123"
  }
}

My goal is to process this request. The trick is that I try to use Builder pattern with optional parameter(according to Effective Java. Joshua Bloch). As I understand I dont have to send all time values. Only sum is required. I try to send Sport object in my RequestBody to my controller by Postman.

So I created POJO class for my Sport:

@Entity
@Table(name = "sport")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Sport {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "date")
    private Date date;

    @Column(name = "city")
    private City city;

    @Type(type = "time")
    private Time time;
}

and Time builder:

package pl.project.model;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

@JsonDeserialize(builder = Time.Builder.class)
public class Time {
    private final String bike;
    private final String run;
    private final String sum;

    private Time(Builder builder) {
        bike = builder.bike;
        run = builder.run;
        sum = builder.sum;

    }

    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder {

        String bike = "-";
        String run = "-";
        final String sum;

        Builder(String sum) {
            this.sum = sum;
        }

        Builder bike(String bike) {
            this.bike = bike;
            return this;
        }

        Builder run(String run) {
            this.run = run;
            return this;
        }

        Time build() {
            return new Time(this);
        }

    }
}

And still getting error:

JSON parse error: Cannot construct instance of `pl.project.model.Time$Builder` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `pl.project.model.Time$Builder` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 14, column: 7] (through reference chain: pl.project.model.Sport[\"time\"])"

I tried many stackoverflow solutions but nothing works for me.

Does anybody could help me?


Solution

  • The Builder class has to have methods named after the property, and/or use @JsonProperty to name the values.

    Using @JsonProperty for constructor parameters

    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder {
        String bike = "-";
        String run = "-";
        final String sum;
        
        Builder(@JsonProperty("sum") String sum) { // must explicitly name the property
            this.sum = sum;
        }
        
        Builder bike(String bike) {
            this.bike = bike;
            return this;
        }
        
        Builder run(String run) {
            this.run = run;
            return this;
        }
        
        Time build() {
            return new Time(this);
        }
    }
    

    Using builder "setter" method

    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder {
        String bike = "-";
        String run = "-";
        String sum;
        
        Builder sum(String sum) { // standard builder "setter" method
            this.sum = sum;
            return this;
        }
        
        @JsonProperty("bike") // must explicitly name for non-standard method name
        Builder setBicycle(String bike) {
            this.bike = bike;
            return this;
        }
        
        Builder run(String run) {
            this.run = run;
            return this;
        }
        
        Time build() {
            if (this.sum == null) // need to validate here, since property is otherwise optional
                throw new IllegalStateException("Required property missing: sum");
            return new Time(this);
        }
    }