Your problem is that you are trying to re-configure `ObjectMapper` after
using it -- this is not supported. Values that you assign before use will
not change (or, rather, not be guaranteed to change, and attempts may even
result in exceptions). You can only specify unchangeable default settings.

However, you can change FilterProvider to use for different calls but you
will need to use `ObjectWriter`, which does allow changing of much of
configuration.

So:

String s = mapper.writer()
    .with(filters)
    .writeValueAsString(list);

you can also hold on to and reuse configured `ObjectWriter` objects, and it
is safe to construct differently configured instances using various
"with()" methods: they create new instances, all of which are immutable and
thread-safe.

Hope this helps,

-+ Tatu +-


On Mon, Aug 22, 2016 at 6:26 PM, <[email protected]> wrote:

> version:jackson 2.7.0
>
> public static void main(String[] args) throws JsonProcessingException {
>         TestData testData1 = new TestData();
>         testData1.setId("1");
>         testData1.setName("11");
>         testData1.setValue("111");
>         TestData testData2 = new TestData();
>         testData2.setId("2");
>         testData2.setName("22");
>         testData2.setValue("222");
>
>         List<TestData> list = new ArrayList<TestData>();
>         list.add(testData1);
>         list.add(testData2);
>
>         ObjectMapper mapper = new ObjectMapper();
>         SimpleFilterProvider filters = new SimpleFilterProvider();
>         
> filters.addFilter("testFilter",SimpleBeanPropertyFilter.filterOutAllExcept(new
>  String[]{"name"}));
> //      
> filters.addFilter("testFilter",SimpleBeanPropertyFilter.serializeAllExcept(new
>  String[] {"name"}));
>         mapper.setFilterProvider(filters);
>
>         String s = mapper.writeValueAsString(list);
>         System.out.println(s);
>     }
>
> output is
> [{"id":"1","name":"11","value":"111"},{"id":"2","name":"22",
> "value":"222"}]
> but i want
> [{"id":"1","value":"111"},{"id":"2",,"value":"222"}]
> and I don't want to use annotations.
>
> Can you tell me how to sovle it?
>
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to