繁体   English   中英

如何基于活动配置文件控制是否应使用Spring Boot序列化字段

[英]How to control if a field should be serialized with Spring Boot based on active profile

我一直在寻找一种方法,该方法能够在Spring Boot中使用自定义注释来注释我的响应模型,以控制应该使用spring概要文件和Jackson来序列化哪个字段。

我知道有一个现有的批注JsonView来定义不同的视图,但这需要逻辑来处理每种模型的单独视图,并启用它们。

我宁愿使用自定义批注,该批注将使用(一组)配置文件来排除/包括该字段。

我还研究了编写一个Serializer,但是它仅控制值,而不控制整个属性,包括名称。

这有可能吗?

class Response {

    var message: String

    @JsonExclude("production")
    var debugMessage: String? = null
}

我建议您在更高的层次上工作。

我想您的响应正在由Webservice序列化。 也许您可以有一个实现使用@JsonExclude发送一个Response,而另一个不发送响应。

您可以使用@Profile批注启用所需的Web服务。

@RestController
@Profile("withExclude")
class WithExcludeController : IMyController {
  fun process() : ResponseWithExclude {
    // ...
  }
}

@RestController
@Profile("withoutExclude")
class WithoutExcludeController : IMyController {
  fun process() : ResponseWithoutExclude {
    // ...
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM