← Back to list

@JsonView Annotation for DTO’s

DTOs (Data Transfer Objects) are used to transfer data between different layers of an application, sending the information which is only…

Necmeddin Tapan in turkcell · 2024-08-16 14:08 · 15 claps · 3.5 min read
#json-view #dto #data-transfer-object #spring #spring-boot
Open on Medium ↗

@JsonView Annotation for DTO’s

DTOs (Data Transfer Objects) are used to transfer data between different layers of an application, sending the information which is only needed. By DTOs, the internal data structure is seperated from the external data representation, which makes the system more secure. Also, DTOs can improve performance by reducing the amount of data being send.

JsonView is an annotation in Jackson that allows to control what parts of a Java object are included in the JSON output. By using JsonView, you can define multiple views for the same model according to different contexts. It allows you to customize the JSON outputs for various use cases and endpoints.

Let’s start with a simple example; Company entity and CompanyDto.

[embed]

CompanyDto is a Data Transfer Object (DTO) used to transfer data related to a Company object.

The “Basic” and “Detail” interfaces define different views. The “Detail” view extends the “Basic” view, meaning it includes everything in “Basic” plus any additional fields marked for “Detail”.

Fields with “@JsonView(Basic.class)” will be included in both the Basic and Detail views, while fields with “@JsonView(Detail.class)” will only be included in the Detail view.

Let’s demonstrate how this works with different endpoints. The first endpoint is “/api/companies”, which will return alist of all companies.

[embed]

@JsonView(CompanyDto.Basic.class) is applied to this method, meaning that, the response will use the “Basic” view of the CompanyDto. So JSON response will include “id” and “name” fields.

Note that, I use ModelMapper (org.modelmapper) in order to map the Company entity to a CompanyDto as seen below. You should add as “@Bean” in your configuration class.

Here is the sample output for “/api/companies” endpoint with @JsonView(CompanyDto.Basic.class) annotation. As seen below, JSON response includes only “id” and “name” fields.

[embed]

The second endpoint for the demonstrations is “/api/companies/{companyId}”, which will return the details of a specific company.

[embed]

@JsonView(CompanyDto.Detail.class) is applied to this method, meaning that the response will use the “Detail” view of the CompanyDto. So JSON response will include “id” and “name” fields from Basic view and also “address” field from Detail view.

Here is the sample output for “/api/companies/{companyId}” endpoint with @JsonView(CompanyDto.Detail.class) annotation. As seen below, JSON response includes “id”, “name” and “address” fields.

[embed]

In this section, we will explore how “JsonView” handles relations in entity classes, such as one-to-many relationships. Similar to previous examples, “JsonView” allows for precise control over which elements of related collections are included in the JSON output.

Let’s create an “Employee” class its corresponding “EmployeeDto”. The Company entity will have a <Set> of Employee objects, reflecting a one-to-many relationship.

[embed]

[embed]

As seen above, the syntax, ”@JsonView({Basic.class, CompanyDto.Detail.class})” in the EmployeeDto allows a single field to be visible in multiple contexts, depending on which view is applied. This means that in addition to being included in the “Basic” view for “EmployeeDto”, the “username” field will also be included in the CompanyDto JSON output when detailed information about a company is requested (triggering the “CompanyDto.Detail” view).

Let’s update the Company and CompanyDto to reflect a more detailed view of the relationship with Employee entities.

[embed]

[embed]

As seen above in the CompanyDto and EmployeeDto classes, each DTO references the other, enabling a detailed representation of their relationship in JSON outputs. This setup allows for flexible control over which details are included, depending on the context and the JsonView applied.

In the CompanyDto class, the “EmployeeDto.Basic.class” view is added to the “name” field. This ensures that the company name is visible in the Basic view and also when employees are requested with the EmployeeDto.Basic view. The Set<EmployeeDto> employees are included only in the “Detail” view, showing a detailed list of EmployeeDto objects. However, not all fields of EmployeeDto will be shown—only those fields that are added to the “CompanyDto.Detail.class” view will be included in the JSON output.

To clarify how fields are returned for different views in the /api/companies/{companyId} endpoint, let’s look at sample JSON responses for both the EmployeeDto.Basic and CompanyDto.Detail views.

Examples with sample JSON responses will clarify which fields are returned for different views of employees and companies. We have updated CompanyDto by adding fields to the “CompanyDto.Detail.class” view, with no changes made to the CompanyDto.Basic.class view. So, it is enough to examine the “/api/companies/{companyId}” endpoint:

[embed]

As seen above, the “employees” field in the JSON response includes only some fields of EmployeeDto. Specifically, only the “username” field is included, as it is annotated with @JsonView(CompanyDto.Detail.class) in the EmployeeDto. Other fields in EmployeeDto are not included in the JSON response because they are not marked with this view.

Let’s examine the new sample for the EmployeeDto with the endpoint “/api/employees”, which requests all employees:

[embed]

@JsonView(EmployeeDto.Basic.class) is applied to this method, meaning that, the response will use the “Basic” view of the EmployeeDto. Consequently, JSON response will include “id”, “username” and “company” fields. However, for the “company” field within the EmployeeDto, the JSON response will include only those fields from CompanyDto that are annotated with the “EmployeeDto.Basic.class” view.

[embed]

As seen above, the “company” field in the JSON response includes only the “name” field from CompanyDto. This is because the “EmployeeDto.Basic.class” view specifies which fields from CompanyDto are included, and in this case, only the “name” field is annotated with @JsonView(EmployeeDto.Basic.class).

Hope, it will be helpful…


메타데이터
post_id
40b6ee04e9b7
slug
jsonview-annotation-for-dtos-40b6ee04e9b7
url
https://medium.com/turkcell/jsonview-annotation-for-dtos-40b6ee04e9b7
canonical_url
https://medium.com/turkcell/jsonview-annotation-for-dtos-40b6ee04e9b7
author_url
https://medium.com/@ntapan
status
ok
fetched_at
2026-07-23 03:07:13