在 application.yml 的 json 响应中包含/排除属性
我正在使用 JHipster(spring boot) 来生成我的项目。我想隐藏/显示 application.yml 中 JSON 中的字段。例如:
我有以下类
@Entity
@Table(name = "port")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Port implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@Column(name = "id")
private Long id;
@Column(name = "city")
private String city;
@Column(name = "description")
private String description;
//getters & setters
}
我的 GET 方法返回如下响应:
{
"id": 1,
"city": "boston",
"description": "test test"
}
我希望能够从 application.yml 中包含/排除某些字段(因为我没有 application.properties),否则会有类似的内容
//application.yml
include: ['city']
exclude: ['description']
:这个例子我的json应该是这样的:
{
"id": 1,
"city": "boston",
}
例如,如果我有40个字段,并且我需要隐藏10个并显示30个,我只想将我想隐藏的10个放在application.yml中的排除中,而不需要每次都去更改代码。我猜@jsonignore 隐藏字段,但我不知道如何从 application.yml 中做到这一点,
抱歉没有解释清楚。我希望一切都清楚。
预先感谢您提供任何建议或解决方案来执行类似的操作
I am using JHipster(spring boot) to generate my project. I would like to hide/show fields in JSON from application.yml. for exemple:
I have the following class
@Entity
@Table(name = "port")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Port implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@Column(name = "id")
private Long id;
@Column(name = "city")
private String city;
@Column(name = "description")
private String description;
//getters & setters
}
My GET method return a response like:
{
"id": 1,
"city": "boston",
"description": "test test"
}
I would like to be able to include/exclude some fields from application.yml (since i don't have application.properties) otherwise to have something like:
//application.yml
include: ['city']
exclude: ['description']
in this exemple my json should look like:
{
"id": 1,
"city": "boston",
}
for exemple if I have 40 fields and I need to hide 10 and show 30 I just want to put the 10 I want to hide in exclude in application.yml without go everytime to change the code. I guess @jsonignore hide fields but I don't know how to do it from application.yml
Sorry for not explaining well. I hope it's clear.
Thank you in advance for any suggestion or solution to do something similar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Spring boot 默认使用 Jackson JSON 库将类序列化为 Json。在该库中,有一个注释
@JsonIgnore
,它的用途正是告诉 Json 引擎忽略序列化/反序列化中的特定属性。因此,假设在您的实体Port
中,您希望从显示中排除房产城市。您所要做的就是使用@JsonIgnore
注释来注释该属性(或其 getter 方法):Spring boot by default uses Jackson JSON library to serialize your classes to Json. In that library there is an annotation
@JsonIgnore
which is used precisely for the purpose to tell Json engine to egnore a particular property from serialization/de-serialization. So, lets say in your entityPort
you would want to exclude property city from showing. All you have to do is to annotate that property (or its getter method) with@JsonIgnore
annotation:您可以尝试在控制器中创建哈希图来管理 HTTP 响应。
You can try to create a hashmap in your controller to manage your HTTP response.
基本上,您不会在 REST 控制器中公开您的
Port
实体,而是公开一个 DTO(数据传输对象),您使用一个简单的类(例如PortMapper< /代码>)。正如其他答案中所建议的,
PortDTO
也可以是Map
。然后,您的服务层可以使用从
application.yml
中获取值的配置对象(例如PortMapperConfiguration
),并由PortMapper
用于有条件地调用PortDTO
setters 来自Port
getters。Basically you don't expose your
Port
entity in your REST controller, you expose a DTO (Data Transfer Object) that you value from your entity in service layer using a simple class (e.gPortMapper
).PortDTO
could also be aMap
as suggested in other answer.Your service layer can then use a configuration object (e.g.
PortMapperConfiguration
) that is valued fromapplication.yml
and used byPortMapper
to conditionally callPortDTO
setters fromPort
getters.