Java Reference
In-Depth Information
This method will build the
link
instance with a relativized URI based on the underlying URI
of the
Link.Builder
and the passed-in
uri
parameter. For example:
Link link
=
Link
.
fromUri
(
"a/d/e"
)
.
rel
(
"update"
).
type
(
"text/plain"
)
.
buildRelativized
(
new
new
URI
(
"a"
));
The URI is calculated internally like this:
URI base
=
new
new
URI
(
"a"
);
URI supplied
=
new
new
URI
(
"a/d/e"
);
URI result
=
base
.
relativize
(
supplied
);
So, the
String
representation of the
link
variable from the example would be:
<
d
/
e
>;
rel
=
"update"
;
type
=
"text/plain"
You can also use the
baseUri()
methods to specific a base URI to prefix your link's URI.
Take this, for example:
Link link
=
Link
.
fromUri
(
"a/d/e"
)
.
rel
(
"update"
).
type
(
"text/plain"
)
.
baseUri
(
"http://localhost/"
)
.
buildRelativized
(
new
new
URI
(
"http://localhost/a"
));
This example code would also output:
<
d
/
e
>;
rel
=
"update"
;
type
=
"text/plain"
Writing Link Headers
Built
Link
instances can be used to create
Link
headers. Here's an example:
@Path
@GET
Response
get
() {
Link link
=
Link
.
fromUri
(
"a/b/c"
).
build
();
Response response
=
Response
.
noContent
()
.
links
(
link
)
.
build
();
return
return
response
;
}
Just build your
Link
and add it as a header to your
Response
.