Home > OS >  Include/exclude Attributes in json response from application.yml
Include/exclude Attributes in json response from application.yml

Time:03-07

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

CodePudding user response:

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;

CodePudding user response:

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;
    }
}

CodePudding user response:

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;
  • Related