What is the correct way to hide properties in a specific Page response while keeping the properties on different responses?
In my Spring Boot API, I have an entity MyEntity
, a controller with 2 endpoints /myentities
and /myentities/{id}
.
/myentities
returns aPage<MyEntity>
response./myentities/{id}
returns a singleMyEntity
object with the id {id}:
class MyEntity:
// MyEntity:
@Entity
@Table(name = "myentities")
public class MyEntity extends AuditModel {
@Id
@Column(name = "id", updatable=false)
private long id;
@Size(min = 3, max = 100)
private String title;
@Column(columnDefinition = "text")
private String content;
public MyEntity() {
super();
}
public MyEntity(String title, String alias, String content) {
super();
this.title = title;
this.content = content;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
RestController with the 2 endpoints:
// controller methods:
@RequestMapping(value = "/myentities", method = RequestMethod.GET)
public Page<MyEntity> getPagedMyEnities(
@RequestParam(name = "after", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") Date after,
@RequestParam(name = "before", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") Date before,
@RequestParam(name = "title", required = false) String title,
@PageableDefault(sort = { "createdAt" }, direction = Sort.Direction.DESC, page = 0, value = 10) Pageable pageable) {
// ..createdAfter(),createdBefore(),titleContains() skipped..
Specification<MyEntity> spec =
where((createdAtAfter(after))
.and(createdAtBefore(before))
.and(titleContains(title));
// MyEntityRepository extends JpaRepository<MyEntity, Long>, JpaSpecificationExecutor<MyEntity>
return this.myEntityRepository.findAll(spec, pageable);
}
@RequestMapping(value = "/myentities/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getMyEntity(@PathVariable(value = "id") long id) {
return ResponseEntity.status(HttpStatus.OK)
.body(this.myEntityRepository.findById(id));
}
Everything works fine, but the content
property of MyEntity is a huge string slowing down the response times for /myentities
a lot. I want to hide the content
property for the page response now and keep including it only for the /myentities/{id}
response.
I tried a few attempts, all missleading:
@JsonIgnore
/@JsonIgnoreProperties
onprivate string content;
"JsonIgnoring" the property will ignore
content
on both endpoints.a) Implementing my own
@JsonComponent MyEntitySerializer extends JsonSerializer<MyEntity>
The MyEntitySerializer does simply only write those fields, that I want to have serialized in my responses. This serializer approach serializes the properties for both endpoints as well..
b) Implementing my own
@JsonComponent MyEntityPageSerializer extends JsonSerializer<Page<MyEntity>>
The PageSerializer for MyEntity iterates through the content of the page and serializes all the properties except of
content
. This approach sort of works leading to Page response missing thecontent
properties for its MyEntity's while still keeping thecontent
property in the single/myentities/{id}
response for a single MyEntity.Badly,
MyEntitySerializer extends Page<MyEntity>
will be used for any genericPage<T>
response and then throwing exceptions for Page responses any other thanMyEntity
.
What is the correct way to ignore JsonProperties in a specific Page response while keeping the properties on different requests?