简体   繁体   中英

How to hide a JsonProperty in the DTO?

I am using a DTO for both the insertion of an object and for the search that returns the data.

in particular, I was interested to understand if there is a way to hide some fields:

    private List <aDTO> sos;

    private List <aDTO> sosPast;

    private List <aDTO> sosPresentFuture;

sos is used in writing to insert a list of objects. in the research, I need two separate elements, one that returns the past objects, one that returns the present and/or future objects.

so in insertion, I will have to show only

    private List <aDTO> sos;

in the research, I will have to show only

    private List <aDTO> sosPast;
    private List <aDTO> sosPresentFuture;

(In research the problem was solved by inserting a

    @Mapping(target = "sos", ignore = true)

in the mapper)

Now I need something to hide the two lists (sosPast and sosPresentFuture) from the Swagger in order to leave only the sos list.

It would be nice to be able to ignore source, it would be the inverse of ignoring the target.

I seem to have solved the problem by doing this:

@JsonProperty("sosPast")
@ReadOnlyProperty
@ApiModelProperty(hidden = true)
private List<aDTO> sosPast;

@JsonProperty("sosPresentFuture")
@ReadOnlyProperty
@ApiModelProperty(hidden = true)
private List <aDTO> sosPresentFuture;
  1. If you want to hide something from Swagger you can use @ApiModelProperty(hidden = true) .
  2. If you want to completely remove the field from being serialized by jackson you can do @JsonIgnore on your response DTO.

Does this help?

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