Commits

Micha Kops committed e41133a Draft

Initial import.

  • Participants

Comments (0)

Files changed (10)

+.settings
+.project
+target
+bin
+infinitest.filters
+.classpath
+# Creating a REST Client Step-by-Step using JAX-RS, JAX-B and Jersey
+
+The following tutorial shows a quick approach using JAX-RS with its reference implementation, Jersey in combination with JAX-B for annotation driven marshalling between XML and our beans.
+
+Please feel free to take a look at [my blog] for the full article.
+
+---
+
+** 2012 Micha Kops / hasCode.com**
+
+   [my blog]:http://www.hascode.com/2010/11/creating-a-rest-client-step-by-step-using-jax-rs-jax-b-and-jersey/
+
+
+<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>com.hascode.tutorial</groupId>
+	<artifactId>rest-client-sample</artifactId>
+	<version>0.0.1</version>
+	<url>http://www.hascode.com</url>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>com.sun.jersey</groupId>
+			<artifactId>jersey-client</artifactId>
+			<version>1.15</version>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.10</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+
+</project>

File src/main/java/com/hascode/tutorial/rest/bean/CustomerBean.java

+package com.hascode.tutorial.rest.bean;
+
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "customer")
+public class CustomerBean {
+	private Long	id;
+	private String	name;
+	private String	email;
+	private String	website;
+
+	@XmlAttribute
+	public Long getId() {
+		return id;
+	}
+	public void setId(Long id) {
+		this.id = id;
+	}
+	public String getName() {
+		return name;
+	}
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	@XmlElement(name = "customer_email")
+	public String getEmail() {
+		return email;
+	}
+	public void setEmail(String email) {
+		this.email = email;
+	}
+	public String getWebsite() {
+		return website;
+	}
+	public void setWebsite(String website) {
+		this.website = website;
+	}
+}

File src/main/java/com/hascode/tutorial/rest/service/RestClient.java

+package com.hascode.tutorial.rest.service;
+
+import java.util.Collection;
+
+import com.hascode.tutorial.rest.bean.CustomerBean;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.GenericType;
+import com.sun.jersey.api.client.WebResource;
+
+public class RestClient {
+	public CustomerBean getCustomer() {
+		Client client = new Client();
+
+		WebResource webResource = client
+				.resource("http://localhost/customer.xml");
+		return webResource.get(CustomerBean.class);
+	}
+
+	public Collection<CustomerBean> getAllCustomers() {
+		GenericType<Collection<CustomerBean>> customerType = new GenericType<Collection<CustomerBean>>() {
+		};
+
+		Client client = new Client();
+
+		WebResource webResource = client
+				.resource("http://localhost/customers.xml");
+		return webResource.get(customerType);
+	}
+
+	public Collection<CustomerBean> getAllCustomersByJson() {
+		GenericType<Collection<CustomerBean>> customerType = new GenericType<Collection<CustomerBean>>() {
+		};
+
+		Client client = new Client();
+
+		WebResource webResource = client
+				.resource("http://localhost/customers.json");
+		return webResource.get(customerType);
+	}
+}

File src/main/resources/customer.xml

+<?xml version="1.0" ?>
+<customer id="123">
+  <name>I R testuser</name>
+  <customer_email>test@hascode.com</customer_email>
+  <website>http://www.hascode.com</website>
+</customer>

File src/main/resources/customers.xml

+<?xml version="1.0" ?>
+<customers>
+ <customer id="123">
+  <name>I R testuser</name>
+  <customer_email>test@hascode.com</customer_email>
+  <website>http://www.hascode.com</website>
+ </customer>
+ <customer id="456">
+  <name>Mee testuser too</name>
+  <customer_email>test2@hascode.com</customer_email>
+  <website>http://www.hascode.com/tag/jax-rs</website>
+ </customer>
+ <customer id="789">
+  <name>some developer</name>
+  <customer_email>test3@hascode.com</customer_email>
+  <website>http://www.hascode.com/about</website>
+ </customer>
+</customers>

File src/test/java/com/hascode/tutorial/rest/service/RestClientTest.java

+package com.hascode.tutorial.rest.service;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.junit.Test;
+
+import com.hascode.tutorial.rest.bean.CustomerBean;
+
+public class RestClientTest {
+	@Test
+	public void testGetCustomer() {
+		RestClient client = new RestClient();
+		CustomerBean customer = client.getCustomer();
+		assertNotNull(customer);
+		assertTrue(123l == customer.getId());
+		assertEquals("I R testuser", customer.getName());
+		assertEquals("test@hascode.com", customer.getEmail());
+		assertEquals("http://www.hascode.com", customer.getWebsite());
+	}
+
+	@Test
+	public void testGetAllCustomers() {
+		RestClient client = new RestClient();
+		Collection<CustomerBean> customers = client.getAllCustomers();
+		assertNotNull(customers);
+		assertEquals(3, customers.size());
+	}
+}

File src/test/resources/customers.json

+{
+	"customers":[
+		{
+			"id":123,
+			"name":"I R testuser",
+			"customer_email":"test@hascode.com",
+			"website":"http://www.hascode.com"
+		},
+		{
+			"id":456,
+			"name":"Mee testuser too",
+			"customer_email":"test2@hascode.com",
+			"website":"http://www.hascode.com/tag/jax-rs"
+		},
+		{
+			"id":789,
+			"name":"some developer",
+			"customer_email":"test3@hascode.com",
+			"website":"http://www.hascode.com/about"
+		}
+	]
+}

File src/test/resources/customers.xml

+<?xml version="1.0" ?>
+<customers>
+	<customer id="123">
+		<name>I R testuser</name>
+		<customer_email>test@hascode.com</customer_email>
+		<website>http://www.hascode.com</website>
+	</customer>
+	<customer id="456">
+		<name>Mee testuser too</name>
+		<customer_email>test2@hascode.com</customer_email>
+		<website>http://www.hascode.com/tag/jax-rs</website>
+	</customer>
+	<customer id="789">
+		<name>some developer</name>
+		<customer_email>test3@hascode.com</customer_email>
+		<website>http://www.hascode.com/about</website>
+	</customer>
+</customers>