JsonView Sample Code

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JsonView Sample Code

budi.hartanto
Hi, I'm using Dandelion 1.0.1, thymeleaf in spring boot. Is there any sample code to use JsonView in Dandelion ? Here is my code
@RequestMapping(value = "/category/datatable")
@ResponseBody
@JsonView(View.Datatable.class)
public DatatablesResponse<Category> findAllForDataTablesFullSpring(@DatatablesParams DatatablesCriterias criterias) {
	DataSet<Category> dataSet = datatableService.generateWithDatatablesCriterias(criterias, "select p from Category p");
	return DatatablesResponse.build(dataSet, criterias);
}
I got empty json. Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JsonView Sample Code

budi.hartanto
I got the solution although it isn't the best practice. Create 2 separate model and in this case I use 'CategoryView' as my datatable api and the others as my RestAPI. So we can control the projection data without JsonView.

@Entity
@Table(name="category")
public class Category {
	@ManyToOne
    	@JsonBackReference
    	private Category parent;
    
	@OneToMany(mappedBy="parent")
        @JsonManagedReference
	private List<Category> child;
}

@Entity
@Table(name="category")
public class CategoryView {
	@ManyToOne
    	@JsonManagedReference
    	private Category parent;
    
	@OneToMany(mappedBy="parent")
        @JsonBackReference
	private List<Category> child;
}
Loading...