0

I have a Spring boot application where I am reading a JSON property file with @ConfigurationProperties annotation:

@Component
@ConfigurationProperties(prefix = "my-config")
@RefreshScope
public class MyConfig {

    private List<Service> services;

    private List<Consumer> consumers;

    ...

Here I would like to add validation while Spring Boot load the property file if something is null or minimum value in the array/list is 1 etc..

I know Spring Boot is using Jackson in the background to perform marshall/unmarshalling between JSON and POJO. I cannot find anything in Jackson which enforce this validation.

Different forums suggest the standard JSR-303 validation however it only works with Rest APIs and not while loading the properties.

1

1 Answer 1

1

you can validate your properties with JSR-303 annotations like that:

@Validated
@Component
@ConfigurationProperties(prefix = "my-config")
@RefreshScope
public class MyConfig {

    @NotNull
    @NotEmpty
    @Size(min = 1, max = 5)
    private List<Service> services;

    private List<Consumer> consumers;
...

and so on.

@Validated enable validation every time annotated field get a value

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Luke - it works! I was missing @Validated annotation :)
You are wellcome :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.