Commits

Micha Kops committed 9b38c44 Draft

JSON mapping example added.

  • Participants
  • Parent commits e41133a

Comments (0)

Files changed (6)

 
 	<properties>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<jersey.version>1.15</jersey.version>
 	</properties>
 
 	<dependencies>
 		<dependency>
 			<groupId>com.sun.jersey</groupId>
 			<artifactId>jersey-client</artifactId>
-			<version>1.15</version>
+			<version>${jersey.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>com.sun.jersey</groupId>
+			<artifactId>jersey-json</artifactId>
+			<version>${jersey.version}</version>
 		</dependency>
 		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
 			<version>4.10</version>
+			<scope>test</scope>
 		</dependency>
 	</dependencies>
 

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

 public class RestClient {
 	public CustomerBean getCustomer() {
 		Client client = new Client();
-
 		WebResource webResource = client
-				.resource("http://localhost/customer.xml");
+				.resource("http://localhost:8080/customer.xml");
 		return webResource.get(CustomerBean.class);
 	}
 
 		};
 
 		Client client = new Client();
-
 		WebResource webResource = client
-				.resource("http://localhost/customers.xml");
+				.resource("http://localhost:8080/customers.xml");
 		return webResource.get(customerType);
 	}
 
-	public Collection<CustomerBean> getAllCustomersByJson() {
-		GenericType<Collection<CustomerBean>> customerType = new GenericType<Collection<CustomerBean>>() {
-		};
-
+	public CustomerBean getCustomerByJson() {
 		Client client = new Client();
-
 		WebResource webResource = client
-				.resource("http://localhost/customers.json");
-		return webResource.get(customerType);
+				.resource("http://localhost:8080/customer.json");
+		return webResource.get(CustomerBean.class);
 	}
 }

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

 		assertNotNull(customers);
 		assertEquals(3, customers.size());
 	}
+
+	@Test
+	public void testGetAllCustomersAsJson() {
+		RestClient client = new RestClient();
+		CustomerBean customer = client.getCustomerByJson();
+		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());
+	}
 }

File src/test/resources/customer.json

+{
+	"@id":123,
+	"name":"I R testuser",
+	"customer_email":"test@hascode.com",
+	"website":"http://www.hascode.com"
+}

File src/test/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/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"
-		}
-	]
-}