Spring Rest动态地从序列化中排除对象属性 [英] spring rest dynamically exclude Object properties from serialization
问题描述
如果A是我所有属性的集合:一次我需要将B暴露给B B A,另一次将C暴露给C⊂A.并且B∩C!=∅
这将导致复杂的视图声明以及对每个类的注释,并且在某些情况下也可能无法实现.所以我想做的就是这样:
@RequestMapping("/test1")
@JsonIgnoreProperties( { "property1"})
public TestObject test1(HttpRequest request){
return new TestObject();
}
@RequestMapping("/test2")
@JsonIgnoreProperties( { "property2"})
public TestObject test1(HttpRequest request){
return new TestObject();
}
输出:
{property2:ipsum,property3:dolor}
{property1:lorem,property3:dolor}
在我看来, Jackson View 是您需要什么.
您必须定义三个应该涵盖所有属性的接口:
- 公共-所有常用属性.
- A-属于集合A的属性.
- B-属于集合B的属性.
示例界面:
class Views {
static class Public { }
static class A extends Public { }
static class B extends Public { }
}
假设您的POJO
类看起来像这样:
class TestObject {
@JsonView(Views.A.class) String property1;
@JsonView(Views.B.class) String property2;
@JsonView(Views.Public.class) String property3;
}
现在,您的控制器应包含以下带有注释的方法:
@RequestMapping("/test1")
@JsonView(Views.B.class)
public TestObject test1(HttpRequest request){
return new TestObject();
}
@RequestMapping("/test2")
@JsonView(Views.A.class)
public TestObject test2(HttpRequest request){
return new TestObject();
}
我创建的所有这些都未经测试.仅阅读文档,但它应该对您有用.我确信类似的解决方案一次对我有用.
i want to exclude specific properties of spring rest response body. after hours of googling around i found this: http://www.jroller.com/RickHigh/entry/filtering_json_feeds_from_spring due to its date i like to ask if there is something more up-to-date for jackson and or fasterxml. JsonView doesnt fit my requirements as i need to have such case covered:
if A is the set of all my attributes: one time i need to expose B with B ⊂ A. another time C with C ⊂ A. And B ∩ C != ∅
this would cause complex view declarations as well as annotating every class and might not be possible as well in some cases. so what i would like to do is something similar to this:
@RequestMapping("/test1")
@JsonIgnoreProperties( { "property1"})
public TestObject test1(HttpRequest request){
return new TestObject();
}
@RequestMapping("/test2")
@JsonIgnoreProperties( { "property2"})
public TestObject test1(HttpRequest request){
return new TestObject();
}
with output:
{property2:ipsum,property3:dolor}
{property1:lorem,property3:dolor}
In my opinion Jackson View is what you need.
You have to define three interfaces which should cover all properties:
- Public - all common properties.
- A - properties which belong to set A.
- B - properties which belong to set B.
Example interfaces:
class Views {
static class Public { }
static class A extends Public { }
static class B extends Public { }
}
Assume that your POJO
class looks like this:
class TestObject {
@JsonView(Views.A.class) String property1;
@JsonView(Views.B.class) String property2;
@JsonView(Views.Public.class) String property3;
}
Now, your controller should contain below methods with annotations:
@RequestMapping("/test1")
@JsonView(Views.B.class)
public TestObject test1(HttpRequest request){
return new TestObject();
}
@RequestMapping("/test2")
@JsonView(Views.A.class)
public TestObject test2(HttpRequest request){
return new TestObject();
}
All of this I has created without testing. Only by reading documentation but it should work for you. I am sure that similar solution worked for me once.
这篇关于Spring Rest动态地从序列化中排除对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!