jackson - 有没有办法在运行时避免父实体的序列化? [英] jackson - Is there any way to avoid serialization of parent entity at runtime?
问题描述
假设我有两个简单的类
@JsonFilter("filter properties by name")
public class Foo
{
private Integer id;
private String name;
}
@JsonFilter("filter properties by name")
public class Bar
{
private Integer id;
private String name;
private Foo foo;
}
我想序列化 Bar的实例
其中 foo
字段只有其id。这一切都应该在运行时完成。
我尝试使用过滤器
I'd like to serialize an instance of Bar
in which the foo
field has only its id. It all should be done at runtime.
I tried to do this using filters
FilterProvider filter = new SimpleFilterProvider().addFilter(
"filter properties by name", SimpleBeanPropertyFilter
.serializeAllExcept(/*name of the field to exclude*/));
objectMapper.writer(filter).writeValuAsString(bar);
但是,这样做,我必须手动排除父类的所有字段;此外,如果此字段之一与子类的字段具有相同的名称,则它们都被排除。
在我的示例中,我不能以这种方式排除字段 name
,因为它也影响字段 name
Bar
类。
However, in doing so, I have to manually exclude all the fields of the parent class; also, if one of this field has the same name of a field of the child class, they are both excluded.
In my example, I can't exclude the field name
in this way because it impacts also the field name
of the Bar
class.
那么,我该如何以最简洁/优雅的方式解决?
有没有类似上面的代码,可能使用点符号或类似的东西?
So, how can I solve in a most concise/elegant way? Is there something similar to the above code, maybe using dot notations or something like that?
例如。直到现在在我的示例过滤器中,我能够写出类似 [...]。serializeAllExcept(name,foo));
,但它很棒拥有 [...]。serializeAllExcept(foo.name));
或类似。
E.g. until now in my example filter I'm able to write something like [...].serializeAllExcept("name", "foo"));
, but it was great to have [...].serializeAllExcept("foo.name"));
or similar.
推荐答案
您不需要任何过滤器。 Foo和Bar类没有变化。
You don't need any filters for it. No changes in Foo and Bar classes.
新的MixInFoo类:
New MixInFoo class:
public class MixInFoo {
@JsonProperty("mixinFooId")
private Integer id;
@JsonIgnore
private String name;
}
我更改了'id'属性名称只是为了说明您可以完全更改响应而无需修改原始Foo类。
I've changed 'id' property name just to illustrate that you can change response completely without modifying original Foo class.
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.getSerializationConfig().addMixInAnnotations(Foo.class, MixInFoo.class);
objectMapper.getDeserializationConfig().addMixInAnnotations(Foo.class, MixInFoo.class);
String result = objectMapper.writeValueAsString(json);
您需要注册MixIn类,如图所示。
You will need to register MixIn classes as shown.
******* 基于过滤器的实施 ***********
******* Filter based implementation ***********
是的,你可以使用Filter来实现相同的结果。您需要将@JsonFilter添加到Foo类并将其命名为FooFilter。然后你可以添加仅适用于Foo类的Filter:
Yes, you can use Filter to achieve the same result. You need to add @JsonFilter to the Foo class and name it like "FooFilter". Then you can add Filter that will apply only to the Foo class:
@JsonFilter("FooFilter")
public class Foo {
private Integer id;
private String name;
}
public class Bar {
private Integer id;
private String name;
private Foo foo;
}
public static void main(String []args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String []fooIgnore = {"name"};
SimpleBeanPropertyFilter propertyFilter = SimpleBeanPropertyFilter.serializeAllExcept(fooIgnore);
FilterProvider filterProvider = new SimpleFilterProvider().addFilter("FooFilter", propertyFilter);
ObjectWriter objectWriter = objectMapper.writer(filterProvider);
String result = objectWriter.writeValueAsString(json);
System.out.println(result);
}
在该实现中,您不需要将自定义SimpleBeanProvider扩展和实现为仅将过滤器应用于Foo类。
In that implementation you don't need to extend and implement you custom SimpleBeanProvider to apply the filter only to the Foo class.
这篇关于jackson - 有没有办法在运行时避免父实体的序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!