Given a projection for a given class, is there a way to tell Spring to include all default attribute of the class defined in the types of the Projection annotation ?
Given the 2 entity Class
@Entity
@Data
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String nom;
private String adresseLigne1;
private String adresseLigne2;
private String ville;
@ManyToOne
private Province province;
/* Many other attribute */
}
and
@Entity
@Data
public class Province {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
}
and a Repository for each
@RepositoryRestResource(collectionResourceRel = "Client", path = "Client")
public interface ClientRepository extends PagingAndSortingRepository<Client, Long> {
List<Client> findBy();
}
-
@RepositoryRestResource(collectionResourceRel = "Province", path = "Province")
public interface ProvinceRepository extends PagingAndSortingRepository<Province, Long> {
List<Province> findByName(String name);
}
I get the following default json for Client :
{
"nom" : "Mallowpond High",
"adresseLigne1" : "895 Gonçal Burg",
"adresseLigne2" : "Apt. 450",
"ville" : "Lake Irenehaven",
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8080/api/rest/Client/108"
},
"province" : {
"href" : "http://127.0.0.1:8080/api/rest/Client/108/province"
}
}
}
Is there a way to create a projection that would return all attribute of Client without having to write all the getXXX method for all of the attribute in Client
@Projection(name = "inlineProvince", types = { Client.class })
public interface ClientProjection {
/* A way to tell the projection to include all of Client attribute */
Province getProvince(); // This is the linked object I want to add to my json output as an in-line map (i.e have the behaviour as if it did not have it's own Repository)
}
so that I can get province embedded in my client JSON when calling http://127.0.0.1:8080/api/rest/Client/108?projection=inline:
{
"nom" : "Mallowpond High",
"adresseLigne1" : "895 Gonçal Burg",
"adresseLigne2" : "Apt. 450",
"ville" : "Lake Irenehaven",
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8080/api/rest/Client/108"
},
"province" : {
"href" : "http://127.0.0.1:8080/api/rest/Client/108/province"
}
},
"province" : {
"name" : "Quebec"
}
}
I found that I can do :
@Projection(name = "inline", types = { Client.class })
public interface ClientProjection {
@Value("#{target}")
Client getClient();
Province getProvince(); // This is the linked object I want to add to my json output as an in-line map (i.e have the behaviour as if it did not have it's own Repository)
}
but I get both client & province as top element. i.e. province is not in client :
{
"province" : {
"name" : "quebec"
},
"client" : {
"nom" : "Mallowpond High",
"adresseLigne1" : "895 Gonçal Burg",
"adresseLigne2" : "Apt. 450",
"ville" : "Lake Irenehaven",
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8080/api/rest/Client/108"
},
"province" : {
"href" : "http://127.0.0.1:8080/api/rest/Client/108/province"
}
}
}
}