0

I have a private field with a private getter method (because I hope to prevent other users from using the getter outside this class, while I have a use case for this getter within this class), but I hope the field to be serialized using objectMapper. What is a good way to do it? Would appreciate any idea!

@Data
public class TestClass{
    @Getter(AccessLevel.PRIVATE)
    private String field
} 

1 Answer 1

1

ObjectMapper by default will serialize only public fields, but you can change it using setVisibility method. You can do it like this:

TestClass testClass = new TestClass("field");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
objectMapper.writeValueAsString(testClass);
Sign up to request clarification or add additional context in comments.

Comments

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.