In my application I am trying to create links in the REST like below. links will be attribute for each POJO and for that POJO attribute uri will be available. For example an Order attribute will have consumer and product as member variable for each of them there will be another attribute to represent link .
objectList": [
{
"orderId": "6444",
"links": [
{
"rel": "consumer",
"uri": "http://localhost:8080/restapp/rest/v1/consumers/Consumer_3"
},
{
"rel": "product",
"uri": "http://localhost:8080/restapp/rest/v1/products/product_1"
}
],
"consumerId": "Consumer_3",
"productId": "product_1",
"orderAmount": 67567
} ] `
POJO will look like this . There will be setter and getter methods will be as well.
public class Order {
private String consumerId;
private String orderId;
private Double orderAmount;
private String productId;
private LinkResource consumerLink;
private LinkResource productLink;
protected List<LinkResource> links = new ArrayList<>();}
Now for links getter I can write like below but it will not be scaleable as in future for any new attributes I'll have to call getter of that link to add into links object.
public List<LinkResource> getLinks() {
if(null!=getConumserLink())
{
links.add(getConumserLink());
}
if(null!=getProductLink())
{
links.add(getProductLink());
}
return links;
}
I am thinking of various option to make it generic so that developer only need to add link attribute for the entity ( like below) and links attribute will automatically add that attribute in the links.
public LinkResource getConumserLink() {
if( null!=consumerId)
{
return new LinkResource(Link.fromUri("/restapp/rest/v1/consumers/"+consumerId).baseUri(getBaseUri()).rel("consumer").build());
}
return null;
}
Will custome annotation be a good approach for this ? like @linkable and at run time it will automatically add that attribute into links. I never created custome annotation before so can someone guide me how can I do this ? Also if there can be better approach then let me know.
Aucun commentaire:
Enregistrer un commentaire