Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value with @ParameterObject with Spring

We have an endpoint with a lot of boolean on it, with default values. I wanted to encapsulate those into an object thanks to the @ParameterObject annotation.

However, I haven't found a way to set default values for it. My idea was to have something similar to what @PageableDault does.

Here the endpoint:

public List<Person> getPersons(@ParameterObject SearchCriteria profileSearchCriteria) {
  // beautiful code
}

And here the object (it uses Lombok but shouldn't have an impact I would say :)):

@Builder
@Data
@Jacksonized
public class ProfileSearchCriteria {

    @Builder.Default
    private boolean criteria1 = false;

    @Builder.Default
    private boolean criteria2 = false;

    @Builder.Default
    private boolean criteria3 = false;

Do you know how can I setup default values for the @ParameterObject annotation?

like image 345
Ludovic Mouline Avatar asked Jul 24 '26 19:07

Ludovic Mouline


1 Answers

You need to put @Schema with the default value above the ProfileSearchCriteria fields. Like this:

@Builder
@Data
@Jacksonized
public class ProfileSearchCriteria {

@Builder.Default
@Schema(defaultValue = "false")
private boolean criteria1 = false;

@Builder.Default
@Schema(defaultValue = "false")
private boolean criteria2 = false;

@Builder.Default
@Schema(defaultValue = "false")
private boolean criteria3 = false;
like image 145
Omid Avatar answered Jul 28 '26 16:07

Omid



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!