Adam Bien's Weblog

Tuesday Jul 09, 2013

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!


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Comments:

I suppose there's an error in the title. It should be Jackson/JSON, rather than JAXB.

Posted by Matt Bukowicz on July 10, 2013 at 12:25 AM CEST #

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