Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Builder class does not have build method (name: 'build') - Jackson

This error occurs when objectMapper.convertValue(cityEntity, City.class)) is called.

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Builder class com.example.PostgresApp.dto.City$Builder does not have build method (name: 'build')

package com.example.PostgresApp.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import lombok.*;
import org.apache.commons.lang3.StringUtils;


@Value
@Builder(builderClassName = "Builder")
@JsonDeserialize(builder = City.Builder.class)
public class City {

    String name;

    String description;

    @JsonPOJOBuilder(withPrefix = StringUtils.EMPTY)
    public static class Builder {

    }
}

Service calling repo seems to be where the exception is thrown

    public List<City> getCities(){
        return cityRepo.findAll().stream().map(cityEntity -> objectMapper
                .convertValue(cityEntity, City.class))
                .collect(Collectors.toList());
    }
like image 419
Clancinio Avatar asked Mar 22 '26 04:03

Clancinio


2 Answers

The problem is that Jackson cannot deserialize the object value.

My solution was to add the following annotations to my class:

// constructor with no args
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
// constructor with all args
@AllArgsConstructor
// ignore unknown properties during deserialization
@JsonIgnoreProperties(ignoreUnknown = true)

My class ended up looking like this:

@Getter
@Builder
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyClass {
    private boolean flag;
    private boolean flag2;
    private MyClassA objectA;
    private MyClassB objectB;
}

If you want to read more on why should we use @NoArgsConstructor and @AllArgsConstructor together, here is a good answer.

like image 83
chris Avatar answered Mar 23 '26 21:03

chris


Are You sure You always pass name and description to the class Builder?

I got the same error and In my case I was trying to to use a generated Builder to create an Object but I did not pass all of the arguments, so the generated method was not the one spring was looking for. It was searching the N+1 arguments method, but I was passing only N arguments. In this case it will look for a different method signature that can not find.

like image 43
Prus Avatar answered Mar 23 '26 23:03

Prus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!