Filtering fields of objects dynamically in Spring Saving json Response data

Hi, I have a requirement to dynamically ignore object fields in spring's response to the data reserve [I know that they can be done in a static way using the @JsonIgnore annotation] ideally based on the spring security role. still manageable, but how to dynamically ignore fields in json response is a problem. After some analysis and docs, I think Jackson is a way to go like spring, the rest of the information will provide Jackson setup through jackson and mixins modules http://docs.spring.io/spring-data/rest/docs/current/reference/html /#customizing-sdr.custom-jackson-deserialization .

So, I think that in jackson api this can be done via @jsonFilter, and then return the same thing when ObjectMapper writes an object [more details here http://www.cowtowncoder.com/blog/archives/2011/09/entry_461. html] .

But I'm not sure how this could be related to rest of spring data (basically, the part in which I acan insert filterprovider into spring object for data storage). Let me know if any of this tried or some of the spring data command has an idea.

I will send an answer myself if I can achieve the same.

UPDATE

So, I realized that the way to implement custom filtering is through jackson BeanSerializerModifier. Great help from @cowtowncoder on twitter. Also useful link or holy grails for filtering with jackson http://www.cowtowncoder.com/blog/archives/2011/02/entry_443.html

+4
source share
1 answer

So yes, finally, I was able to solve this problem. The trick here is to use a custom BeanSerializerModifier and register it through the user module [which is a custom click available to configure spring data home jackson data serialization), something like

 setSerializerModifier( new CustomSerializerModifier()).build()));

BeanSerializerModifier, changeProperties, , BeanPropertyWriter .

List<BeanPropertyWriter> included = Lists.newArrayList();
    for (BeanPropertyWriter property : beanProperties)
        if (!filter.contains(property.getName()))
            included.add(property);

, - .

github, https://github.com/gauravbrills/SpringPlayground

+5

Source: https://habr.com/ru/post/1599490/


All Articles