I am using Spring-MVC, Spring-data-jpa, jackson in a Jhipster project.
I managed to use the annotation @JsonViewfor the object, and it works well when the method in the rest of the controller returns a type ResponseEntity<List<MyObject>>, but I cannot get it to work when the type of the returned method ResponseEntity<Page<MyObject>>.
I tried to set MapperFeature.DEFAULT_VIEW_INCLUSIONto true (the default value is false). When I do this, all attributes are serialized. But filtering through @JsonViewno longer works.
I cannot modify the object Pagebecause it is a Spring -data object.
I am looking for a way to tell Jackson to include all the attributes of an object Page.
Here is my code:
My essence:
@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;
}
My break 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:
{}
Page @JsonView , .
Jackson Page, @JsonView.
?