简体   繁体   English

spring boot:将数据发送到1个端点但对其他端点隐藏

[英]spring boot: sending data to 1 endpoint but hiding from other

I have a simple spring boot application, my model contains 3 fields.我有一个简单的 Spring Boot 应用程序,我的模型包含 3 个字段。 and controller exposes 2 endpoints /e1 and /e2.和控制器公开 2 个端点 /e1 和 /e2。 what I want to achieve is that when /e1 is called I want to return all 3 fields from my model by when /e2 is called I was to return just 2 fields and hiding the 3rd field from it.我想要实现的是,当 /e1 被调用时,我想从我的模型中返回所有 3 个字段,当 /e2 被调用时,我只返回 2 个字段并从中隐藏第三个字段。

is there a way to do it and how this can be done ?有没有办法做到这一点以及如何做到这一点? thanks谢谢

First, you should always have 2 classes - entity and its dto presentation - even if they fields are very similar.首先,您应该始终有 2 个类 - 实体及其 dto 表示 - 即使它们的字段非常相似。 This gives you:这给你:

  • independcy from external consumers, you may change your model in future but the contract remains the same独立于外部消费者,您将来可能会更改您的模型,但合同保持不变
  • opportunity to use advantages of Hiberante laze loading有机会利用 Hiberante laze loading 的优势
  • easily solve your issue: just have 2 DTOs, one with all fields, other one without hidden field.轻松解决您的问题:只有 2 个 DTO,一个包含所有字段,另一个没有隐藏字段。

You might need bean mapper for converting entity to DTO.您可能需要bean 映射器将实体转换为 DTO。

Two solutions you can try either of them ..您可以尝试两种解决方案中的任何一种..

  1. Create two POJO's One with 3 field and another with 2 fields and return accordingly.创建两个 POJO,一个有 3 个字段,另一个有 2 个字段,并相应地返回。
  2. If you dont want separate pojo then set value of all 3 fields in pojo and when /e2 called, iterate over it and explicitely blank out that value which you want hide.如果您不想要单独的 pojo,则在 pojo 中设置所有 3 个字段的值,当 /e2 调用时,对其进行迭代并明确地将您想要隐藏的值清空。

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

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