adam bien's blog

Configuring JAXB Mapping On A JAX-RS 2.0 Client 📎

Mapping of POJOs to XML needs to be configured on a JAX-RS 2.0 client with registering a feature class:


import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.junit.Before;
import org.junit.Test;

public class MessagesResourceIT {

    private Client client;
    private WebTarget messagesTarget;

    @Before
    public void initClient() {
        this.client = ClientBuilder.newClient().register(JacksonFeature.class);
        this.messagesTarget = this.client.target("http://.../");
    }

    @Test
    public void allMessages() {
        Message response = this.messagesTarget.path("42").request().get(Message.class);
		//...asserts omitted
    }
}


In addition you will also have to setup the dependency to the underlying framework (Jackson in this case):


<dependency>
	<groupId>org.glassfish.jersey.media</groupId>
	<artifactId>jersey-media-json-jackson</artifactId>
	<version>2.0</version>
</dependency>       

JavaEE 7 JSON processing needs similar configuration as well.

See you at Java EE Workshops at MUC Airport!