在 application.yml 的 json 响应中包含/排除属性

发布于 2025-01-12 03:09:54 字数 1198 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等数载,海棠开 2025-01-19 03:09:54

Spring boot 默认使用 Jackson JSON 库将类序列化为 Json。在该库中,有一个注释 @JsonIgnore ,它的用途正是告诉 Json 引擎忽略序列化/反序列化中的特定属性。因此,假设在您的实体Port 中,您希望从显示中排除房产城市。您所要做的就是使用 @JsonIgnore 注释来注释该属性(或其 getter 方法):

@Column(name = "city")
@JsonIgnore
private String city;

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 entity Port 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:

@Column(name = "city")
@JsonIgnore
private String city;
辞慾 2025-01-19 03:09:54

您可以尝试在控制器中创建哈希图来管理 HTTP 响应。

        Map<String, Object> map = new HashMap<>();

        map.put("id", Port.getId());
        map.put("city", Port.getCity());

        return map;

You can try to create a hashmap in your controller to manage your HTTP response.

        Map<String, Object> map = new HashMap<>();

        map.put("id", Port.getId());
        map.put("city", Port.getCity());

        return map;
我偏爱纯白色 2025-01-19 03:09:54

基本上,您不会在 REST 控制器中公开您的 Port 实体,而是公开一个 DTO(数据传输对象),您使用一个简单的类(例如 PortMapper< /代码>)。正如其他答案中所建议的,PortDTO 也可以是 Map

然后,您的服务层可以使用从 application.yml 中获取值的配置对象(例如 PortMapperConfiguration),并由 PortMapper 用于有条件地调用 PortDTO setters 来自 Port getters。

@ConfigurationProperties(prefix = "mapper", ignoreUnknownFields = false)
public class PortMapperConfiguration {
    private List<String> include;
    private List<String> exclude;

    // getters/setters
}
@Service
public class PortMapper {
    private PortMapperConfiguration configuration;

    public PortMapper(PortMapperConfiguration configuration) {
        this.configuration = configuration;
    }

    public PortDTO toDto(Port port) {
      PortDTO dto = new PortDTO();

      // Fill DTO based on configuration
      return dto;
    }
}

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.g PortMapper). PortDTO could also be a Map as suggested in other answer.

Your service layer can then use a configuration object (e.g. PortMapperConfiguration) that is valued from application.yml and used by PortMapper to conditionally call PortDTO setters from Port getters.

@ConfigurationProperties(prefix = "mapper", ignoreUnknownFields = false)
public class PortMapperConfiguration {
    private List<String> include;
    private List<String> exclude;

    // getters/setters
}
@Service
public class PortMapper {
    private PortMapperConfiguration configuration;

    public PortMapper(PortMapperConfiguration configuration) {
        this.configuration = configuration;
    }

    public PortDTO toDto(Port port) {
      PortDTO dto = new PortDTO();

      // Fill DTO based on configuration
      return dto;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文