Skip to content

Instantly share code, notes, and snippets.

@cpilsworth
Created September 11, 2012 10:56
Show Gist options
  • Save cpilsworth/3697582 to your computer and use it in GitHub Desktop.
Save cpilsworth/3697582 to your computer and use it in GitHub Desktop.
Jersey Client Test
import java.util.Map;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
/**
* Tests the use of Jersey Client to call a service end-point and marshal the JSON result to an object.
*
* Outputs something like:
* <pre>
* {
* url: http://httpbin.org/post?query=arg,
* origin: 80.231.216.11,
* args: {query=arg},
* headers: {Content-Length=11, Connection=keep-alive, Accept=text/html, image/gif, image/jpeg, *; q=.2, *\/*; q=.2, User-Agent=Java/1.6.0_33, Host=httpbin.org, Content-Type=application/x-www-form-urlencoded},
* form: {Hello=World}
* files: {}
* }
* </pre>
*
* @author cpilsworth
*/
@JsonIgnoreProperties(ignoreUnknown=true)
public class ClientTest {
private final String url;
private final Map<String, String> args;
private final Map<String, String> headers;
private final Map<String, String> form;
private final Map<String, String> files;
private final String origin;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Client c = Client.create();
WebResource r = c.resource("http://httpbin.org/post?query=arg");
ClientTest response = r.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientTest.class, formParams());
System.out.println(response);
}
/**
* You can use getters/setters or alternatively inject properties into constructors (like so) allowing for immutable objects.
* @param url
* @param origin
* @param args
* @param headers
* @param form
* @param files
*/
@JsonCreator
public ClientTest(@JsonProperty("url") String url, @JsonProperty("origin") String origin, @JsonProperty("args") Map<String, String> args,
@JsonProperty("headers") Map<String, String> headers, @JsonProperty("form") Map<String, String> form,
@JsonProperty("files") Map<String, String> files) {
this.url = url;
this.origin = origin;
this.args = args;
this.headers = headers;
this.form = form;
this.files = files;
}
private static MultivaluedMap<String, String> formParams() {
MultivaluedMap<String,String> params = new MultivaluedMapImpl();
params.add("Hello", "World");
return params;
}
public String toString() {
return new StringBuilder()
.append("{\n url: ").append(url)
.append(",\n origin: ").append(origin)
.append(",\n args: ").append(args)
.append(",\n headers: ").append(headers)
.append(",\n form: ").append(form)
.append(",\n files: ").append(files)
.append("\n}")
.toString();
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>uk.co.diffa.services</groupId>
<artifactId>ws-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WSClient</name>
<description>Web Services Client using Jersey</description>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment