JAX RS – 36 – javax.ws.rs.core.Link

Merhaba Arkadaslar
Bu bolumde javax.ws.rs.core.Link abstract sinifini inceleyecegiz.

Building Links and Link Headers

Link sinifi JAX-RS 2.0 ile eklendi. Link sinifi ile header olarak ya da XML dokumanina link/hypermedia ekleyebiliriz.

JAX-RS 2.0 added some support to help you build Link headers and to embed links 
in your XML documents through the Link and Link.Builder classes

javax.ws.rs.core.Link

package javax.ws.rs.core;

public abstract class Link {
	public abstract URI getUri();
	public abstract UriBuilder getUriBuilder(); public abstract String getRel();
	public abstract List<String> getRels();
	public abstract String getTitle();
	public abstract String getType();
	public abstract Map<String, String> getParams(); public abstract String toString();

Link instance’lari Link.Builder static nested class yardimiyla olusturulur.
Link.Builder instance’i icin fromUri , fromResource gibi metotlari kullanabiliriz.
Bu metotlarin calisma mantigi UriBuilder.fromXXX metotlarina benzer sekilde olmaktadir.

public abstract class Link {
	public static Builder fromUri(URI uri)
	public static Builder fromUri(String uri)
	public static Builder fromUriBuilder(UriBuilder uriBuilder) 
	public static Builder fromLink(Link link)
	public static Builder fromPath(String path)
	public static Builder fromResource(Class<?> resource)
	public static Builder fromMethod(Class<?> resource, String method)

Link e ait cesitli konfigurasyonlari yapmamiz icin ;

public Builder rel(String rel);
public Builder title(String title);
public Builder type(String type);
public Builder param(String name, String value);

Son olarak Link objesini olusturmak icin ;

public Link build(Object... values);

Simdi de ornek uzerinde inceleyelim;

LinkService.java

package _28.link.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.Response;

@Path("/link-path")
public class LinkService {

	@GET
	@Path("/test1")
	public Response test1LinkMethod() {
		// rel : relation
		// https://tools.ietf.org/html/rfc5988
		Link link1 = Link.fromUri("http://localhost/root/customers/all").rel("section").type("text/plain").build();

		Link link2 = Link.fromUri("http://{host}/root/customers/{id}").rel("self").type("text/plain").build("localhost",
				"1234");

		System.out.println(link1.toString());

		Response response = Response.ok("Check Response Header!").links(link1, link2).build();
		return response;
	}
}

Link#fromUri metodu ile Link objesini olusturduk , Response#links metoduna bu objeleri verdik.
Link bilgisi response header da yer alacaktir.

Embedding Links in XML

Bir baska yaklasim olarak , JAXB XmlAdapter yaklasimini kullanabiliriz.

CustomerResource.java

package _28.link.service;

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import _28.link.model.Customer;

@Path("/customer-link")
public class CustomerResource {

	@GET
	@Path("/customer/{id}")
	@Produces(MediaType.APPLICATION_XML)
	public Customer getCustomer(@PathParam("id") int id) {

		Customer customer = new Customer();
		customer.setId(id);
		customer.setFirstName("levent");
		customer.setLastName("erguder");

		UriBuilder builder = UriBuilder.fromResource(CustomerResource.class).path(CustomerResource.class,
				"updateCustomer");

		Link updateLink = Link.fromUri(builder.build()).rel("update").build();

		//
		builder = UriBuilder.fromResource(CustomerResource.class).path(CustomerResource.class, "deleteCustomer");
		Link deleteLink = Link.fromUri(builder.build()).rel("delete").build();

		//
		List<Link> links = new ArrayList<Link>();

		links.add(updateLink);
		links.add(deleteLink);

		customer.setLinks(links);
		return customer;
	}

	@PUT
	@Path("/customer.update")
	@Consumes(MediaType.APPLICATION_XML)
	public void updateCustomer(Customer customer) {
		// update method
	}

	@DELETE
	@Path("/customer.delete")
	public void deleteCustomer(int id) {
		// update method
	}

}

Kodumuzu inceleyecek olursak getCustomer metodunda ek olarak 2 tane Link objesi olusturduk. Bu List’i XML datamiza ekleyelim.

Ornegimizi calistirdigimizda ;

http://localhost:8080/injavawetrust.jersey.tutorial/customer-link/customer/1
http://localhost:8080/injavawetrust.resteasy.tutorial/customer-link/customer/1

Github kaynak kodlar / source folder
injavawetrust.resteasy
injavawetrust.jersey

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *