Configuring JAX-RS 2.0 Client For JSON Serialization 📎
JAX-RS 2.0 comes with a standardized client, which is also capable of object serialization / deserialization from JSON format. However, a given JSON implementation needs to be registered first. In the sample below the JSON reference implementation (coming with GlassFish v4) is used:
import javax.json.JsonArray;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import org.glassfish.jersey.jsonp.JsonProcessingFeature;
public class RestClient {
public JsonArray get() {
Client client = ClientBuilder.newBuilder().register(JsonProcessingFeature.class)
.property(JsonGenerator.PRETTY_PRINTING, true).build();
WebTarget path = this.client.target("…");
return path.request().get(JsonArray.class);
}
The dependencies are already located in maven central:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
See you at Java EE Workshops at MUC Airport, especially at the Java EE 7 Delta Day!