Link Serialization With JAX-RS 2+

JAX-RS 2+ supports rfc5988 compliant and JAXB-serializable links.

A Link can be treated as an ordinary payload:


import javax.ws.rs.core.Link;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Message {

    @XmlJavaTypeAdapter(Link.JaxbAdapter.class)
    private Link airhacksLink;

    public Message() {
        this.airhacksLink = Link.fromUri("http://airhacks.com").
                rel("workshops").
                build();
    }
}


An entity directly exposed via:

@Path("messages")
public class MessagesResource {

    @GET
    public Message get() {
        return new Message();
    }
}


produces the following output:

<message>
	<airhacksLink href="http://airhacks.com" rel="workshops"/>
</message>

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

Your code uses rel("javaee"), but the output contains rel="workshops". Is that a typo (semantico?) or am I missing something?

Posted by ron on February 18, 2015 at 11:14 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License