I have such a field:

@ManyToOne @JoinColumn(name = "organization_id") @JsonIgnore private Organization organization; 

Usually, I don’t need it to be displayed in json when I give the object through @RestController spring 'a. But in some cases I would like this field to be displayed. Is it possible?

    1 answer 1

    I think the @JsonView annotation will do for you.
    You can read more here.

    As an alternative, you can hang up the @JsonFilter annotation above your entity class. For example:

     @JsonFilter("contract") public class SupplierContract 

    And in the controller class in the method:

     public ResponseEntity<String> getSerializableData(Object data) throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); String filter = "contract"; String[] except = new String[]{"excluded", "fields"}; SimpleFilterProvider provider = new SimpleFilterProvider().addFilter(filter, SimpleBeanPropertyFilter.serializeAllExcept(except)); return new ResponseEntity<>(objectMapper.writer(provider).writeValueAsString(data), HttpStatus.OK); } 

    In this case, all fields specified in the except array will be excluded from json for the filter "contract"

    • The question arises. How do the @JsonIgnore and @JsonView annotations @JsonView together? - faoxis
    • I think you need to use @JsonView separately. To register there only in what case to display this field - Roman Danilov