Over a million developers have joined DZone.

Reading and Writing JAX-RS Link Objects

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

JAX-RS contains a rather nice handy representation of the a Link that can be serialised with and adapter into XML and JSON, unfortunately there is a bug in the JAX-RS spec that means that the standard adapter provided is missing the setters required to deserialise the Link later on.

This isn't a problem if you are using the JAX-B RI as it appears to be more relaxed than the standard; but it will be a problem for other implementations. There is a further bug if you are using MOXy, aka EclipseLink, to produce either JSON or XML that it will fail and just call toString() because it doesn't like the type adapter being an inner class of the class that is being adapted. (Bug TBC)

DirectionJAXB RI + JAX-RS AdapterMOXy + JAX-RS AdapterMOXy + Outer AdapterMOXy + Outer Adapter + SettersMOXy + Inner Adapter (Different class)

The workaround is to create a new adapter as a top level class in order to replace the one provided by the standard. Just quickly it would look something like this:

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.Link;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.namespace.QName;

public class LinkAdapter
    extends XmlAdapter<linkjaxb, link=""> {

    public LinkAdapter() {

    public Link unmarshal(LinkJaxb p1) {
        Link.Builder builder = Link.fromUri(p1.getUri());
        for (Map.Entry<QName, Object> entry : p1.getParams().entrySet()) {
            builder.param(entry.getKey().getLocalPart(), entry.getValue().toString());
        return builder.build();

    public LinkJaxb marshal(Link p1) {
        Map<QName, Object> params = new HashMap<>();
        for (Map.Entry<String,String> entry : p1.getParams().entrySet()) {
            params.put(new QName("", entry.getKey()), entry.getValue());
        return new LinkJaxb(p1.getUri(), params);

With a simple POJO to go with it.

import java.net.URI;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.namespace.QName;

public class LinkJaxb  {

    private URI uri;
    private Map<QName, Object> params;

    public LinkJaxb() {
        this (null, null);

    public LinkJaxb(URI uri) {
        this(uri, null);

    public LinkJaxb(URI uri, Map<QName, Object> map) {
        this.uri = uri;
        this.params = map!=null ? map : new HashMap<QName, Object>();

    @XmlAttribute(name = "href")
    public URI getUri() { 
        return uri;

    public Map<QName, Object> getParams() { 
        return params;

    public void setUri(URI uri) {
        this.uri = uri;

    public void setParams(Map<QName, Object> params) {
        this.params = params;


With this you can read and write the Link objects to and from XML, and JSON with MOXy, to your hearts content.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.


Published at DZone with permission of Gerard Davison, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}