Skip to content

Allow overriding builder's "withPrefix" with new @JsonDeserialize.builderPrefix property #2624

Closed

Description

@kennethjor

It would be nice to be able to configure the builder method name prefix Jackson uses either globally through a configuration on ObjectMapper or through the @JsonDeserialize annotation itself. I'm currently using Lombok to generate my builders for me and to make it work nicely with Jackson I have to write boilerplate code, which is the whole point of lombok.

The Problem

Currently, in order to use Jackson and lombok in tandem, I have to write a model class like this:

@Builder
@JsonDeserialize(builder = Model.ModelBuilder.class)
public class Model {
	
	/** various properties */

	@JsonPOJOBuilder(withPrefix = "")
	public static class ModelBuilder {
	}
}

The issue becomes even worse if using the @SuperBuilder annotation:

@SuperBuilder
@JsonDeserialize(builder = Model.ModelBuilderImpl.class)
public class Model extends AbstractModel {

	/** various properties */

	@JsonPOJOBuilder(withPrefix = "")
	protected static final class ModelBuilderImpl extends Model.ModelBuilder<Model, Model.ModelBuilderImpl> {
	}
}

There's also a blurb about the workaround in Lombok's own documentation.

Related issue: https://github.com/FasterXML/jackson-databind/issues/1997

Global Config

One way to fix it would be to allow this to be configured globally on ObjectMapper like:

objectMapper.configure(DeserializationFeature.POJO_BUILDER_PREFIX, "");

JsonDeserialize Property

Another solution would be to allow it to be set on the @JsonDeserialize annotation itself, like:

@JsonDeserialize(builder = Model.ModelBuilder.class, withBuilderPrefix = "")

Personally, I prefer option 2.

Activity

  1. cowtowncoder commented on Feb 18, 2020

    @cowtowncoder
    Member

    Sounds like a reasonable idea, and I think I know the mechanism it should use (since I prefer not adding one-off configuration methods if at all possible): "config overrides".
    This could even allow per-class non-annotation-based override, possibly.

    I don't know which version this would go in -- 2.11 is not yet out, but not sure I have time to add it there, as I am hoping to wrap it up. But this could probably work out nicely for 2.12.

  2. kennethjor commented on Mar 24, 2020

    @kennethjor
    Author

    FWIW the Lombok team have implemented a @Jacksonized annotation for making Lombok models play nicely with Jackson.

  3. cowtowncoder commented on Mar 24, 2020

    @cowtowncoder
    Member

    @kennethjor Interesting. Thank you for sharing!

  4. cowtowncoder commented on Aug 29, 2020

    @cowtowncoder
    Member

    Ok, with #2800, there now IS a way to, I think, configure builder-"with" prefix, using new AccessNamingStrategy (and provider). And in fact DefaultAccessorNamingStrategy allows configuration, so something like:

        ObjectMapper mapper = JsonMapper.builder()
                    .accessorNaming(new DefaultAccessorNamingStrategy.Provider()
                            .withBuilderPrefix("") // no prefix
    // or                       .withBuilderPrefix("With") //  would expect 'WithX()' etc
                    )
                    .build();
    

    or, if you want something more complicated (multiple prefixes, or different naming convention),
    just implement strategy yourself (and configure with Provider that constructs instances).

  5. cowtowncoder commented on Oct 3, 2020

    @cowtowncoder
    Member

    Added a test to verify that case above (no/empty prefix) works as expected.

    But I think I will leave this open as addition of an annotation in @JsonDeserialize does seem like a good idea, if it can be pulled off.

  6. changed the title [-]Configure withPrefix somewhere other than @JsonPOJOBuilder[/-] [+]Allow overriding builder's "withPrefix" with new `@JsonDeserialize` property[/+] on Oct 3, 2020
  7. added a commit that references this issue on Oct 13, 2020
  8. added a commit that references this issue on Jan 22, 2026
  9. added this to the 3.1.0 milestone on Jan 24, 2026
  10. changed the title [-]Allow overriding builder's "withPrefix" with new `@JsonDeserialize` property[/-] [+]Allow overriding builder's "withPrefix" with new `@JsonDeserialize.builderPrefix` property[/+] on Jan 24, 2026
  11. added a commit that references this issue on Jan 24, 2026
  12. cowtowncoder commented on Jan 24, 2026

    @cowtowncoder
    Member

    Implemented for 3.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.1builder-relatedIssue related to handling of Builder-style deserialization

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions