简体   繁体   中英

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

I have a simple spring boot application, my model contains 3 fields. and controller exposes 2 endpoints /e1 and /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.

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. 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
  • easily solve your issue: just have 2 DTOs, one with all fields, other one without hidden field.

You might need bean mapper for converting entity to 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.
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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