Issue
I'm using Spring-MVC, Spring-data-jpa, jackson on a Jhipster project.
I managed to use the @JsonView
annotation on an object and it works well when the method in the rest controller return a type ResponseEntity<List<MyObject>>
but I can't make it work when the method return type is ResponseEntity<Page<MyObject>>
.
I've tried to set MapperFeature.DEFAULT_VIEW_INCLUSION
to true (which default is false). When I do it, all attributes are serialized. But filtering through @JsonView
does not work anymore.
I can't modify the Page
object because it's a Spring-data object.
I'm looking for a way to tell jackson to include all attributes of the Page
object.
Here is my code:
My entity:
@Entity
@Table(name = "T_REGION")
public class Region implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "code", nullable = false)
private Integer code;
@Column(name = "name", length = 60, nullable = false)
@JsonView(View.Summary.class)
private String name;
// Getters and setters
}
My rest controller:
@RestController
@RequestMapping("/api")
public class RegionResource {
@RequestMapping(value = "/regionsearch1",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@JsonView(View.Summary.class)
public ResponseEntity<Page<Region>> findAll1(
@RequestParam(value = "page" , required = false) Integer offset,
@RequestParam(value = "per_page", required = false) Integer limit,
Sort sort)
throws URISyntaxException {
Pageable pageRequest = PaginationUtil.generatePageRequest(offset, limit, sort);
Page<Region> page = regionRepository.findAll(pageRequest);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/regionsearch1", pageRequest);
return new ResponseEntity<>(page, headers, HttpStatus.OK);
}
@RequestMapping(value = "/regionsearch2",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@JsonView(View.Summary.class)
public ResponseEntity<List<Region>> findAll2(
@RequestParam(value = "page" , required = false) Integer offset,
@RequestParam(value = "per_page", required = false) Integer limit,
Sort sort)
throws URISyntaxException {
Pageable pageRequest = PaginationUtil.generatePageRequest(offset, limit, sort);
Page<Region> page = regionRepository.findAll(pageRequest);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/regionsearch2", pageRequest);
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
}
findAll1
returns:
[
{
"name": "Ile-de-France"
},
{
"name": "Champagne-Ardenne"
},
....
]
findAll2
returns:
{}
The object Page
has no @JsonView
on its attributes therefore no attributes are serialized.
I can't find a way to tell Jackson to include all Page
attributes even when @JsonView
is used.
Any ideas ?
Solution
I've encountered the same problem and I solved it by setting MapperFeature.DEFAULT_VIEW_INCLUSION
to true, but you should annotate all fields in classes where you want to apply your view with JsonView
or JsonIgnore
annotation so they wouldn't be included by default in json.
Answered By - Alexey Belostotsky
Answer Checked By - Marie Seifert (JavaFixing Admin)