Spring Rest动态地从序列化中排除对象属性 [英] spring rest dynamically exclude Object properties from serialization

查看:136
本文介绍了Spring Rest动态地从序列化中排除对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想排除弹簧支座响应体的特定属性.经过数小时的搜寻后,我发现了以下内容: http://www.jroller.com/RickHigh/entry/由于其日期,filtering_json_feeds_from_spring 我想问一下是否有关于jackson和fastxml的最新信息. JsonView不符合我的要求,因为我需要涵盖这种情况:

如果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 是您需要什么.

您必须定义三个应该涵盖所有属性的接口:

  1. 公共-所有常用属性.
  2. A-属于集合A的属性.
  3. 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:

  1. Public - all common properties.
  2. A - properties which belong to set A.
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆