Reputation: 819
Given a relative link, such as "/v1/payments/sale/:saleId", how can "href" be resolved in this example (from the PayPal API docs: https://developer.paypal.com/docs/api/hateoas-links/)
{
"links": [{
"href": "https://api.paypal.com/v1/payments/sale/36C38912MN9658832",
"rel": "self",
"method": "GET"
}, {
"href": "https://api.paypal.com/v1/payments/sale/36C38912MN9658832/refund",
"rel": "refund",
"method": "POST"
}, {
"href": "https://api.paypal.com/v1/payments/payment/PAY-5YK922393D847794YKER7MUI",
"rel": "parent_payment",
"method": "GET"
}]
}
I'm building a RESTful API using Spark and need to make the API "discoverable".
Upvotes: 0
Views: 477
Reputation: 819
The Request.url()
contains the URL for the current request, so using URL(context, spec)
from java.net
constructs the link target URL:
URL current = new URL(request.url()); // some resource at https://api.paypal.com/...
URL target = new URL(current "/v1/payments/sale/:saleId");
// ^^ https://api.paypal.com/v1/payments/sale/:saleId
Upvotes: 0