Commits

Micha Kops committed c276342

Client examples added.

Comments (0)

Files changed (8)

 	</parent>
 	<artifactId>rest-client</artifactId>
 	<packaging>jar</packaging>
+
+	<dependencies>
+		<dependency>
+			<groupId>com.hascode.tutorial.rest</groupId>
+			<artifactId>entity</artifactId>
+			<version>1.0.0-SNAPSHOT</version>
+		</dependency>
+		<dependency>
+			<groupId>org.glassfish.jersey.core</groupId>
+			<artifactId>jersey-client</artifactId>
+			<version>2.5</version>
+		</dependency>
+		<dependency>
+			<groupId>org.glassfish.jersey.media</groupId>
+			<artifactId>jersey-media-json-jackson</artifactId>
+			<version>2.5</version>
+		</dependency>
+		<dependency>
+			<groupId>org.hamcrest</groupId>
+			<artifactId>hamcrest-all</artifactId>
+			<version>1.3</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.11</version>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
 </project>

client/src/test/java/it/ClientExample.java

+package it;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.math.BigDecimal;
+import java.util.GregorianCalendar;
+import java.util.List;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.GenericType;
+import javax.ws.rs.core.MediaType;
+
+import org.glassfish.jersey.jackson.JacksonFeature;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.hascode.tutorial.jaxrs.entity.Book;
+
+public class ClientExample {
+	private static final String REST_SERVICE_URL = "http://localhost:8080/tutorial/rs/book";
+
+	private static final String TITLE = "One big book";
+	private static final BigDecimal PRICE = new BigDecimal("20.0");
+	private static final GregorianCalendar PUBLISHED = new GregorianCalendar(
+			2013, 12, 24);
+
+	Book book = new Book();
+	Client client = ClientBuilder.newClient().register(JacksonFeature.class);
+
+	@Before
+	public void setup() {
+		book.setTitle(TITLE);
+		book.setPrice(PRICE);
+		book.setPublished(PUBLISHED);
+	}
+
+	@Test
+	public void clientShouldCRUDBook() {
+		// 1. Save a new book
+		Book bookPersisted = client
+				.target(REST_SERVICE_URL)
+				.request()
+				.post(Entity.entity(book, MediaType.APPLICATION_JSON),
+						Book.class);
+
+		String bookId = bookPersisted.getId();
+		assertThat(bookId, notNullValue());
+
+		// 2. Fetch book by id
+		Book book2 = client.target(REST_SERVICE_URL).path("/{bookId}")
+				.resolveTemplate("bookId", bookId).request().get(Book.class);
+		assertThat(book2, notNullValue());
+		assertThat(book2.getTitle(), equalTo(TITLE));
+		assertThat(book2.getPrice(), equalTo(PRICE));
+		assertThat(book2.getPublished().getTime(), equalTo(PUBLISHED.getTime()));
+
+		// 3. Fetch all books
+		GenericType<List<Book>> bookType = new GenericType<List<Book>>() {
+		};
+		List<Book> books = client.target(REST_SERVICE_URL).request()
+				.get(bookType);
+		assertThat(books.size(), equalTo(1));
+
+		// 4. Delete a book
+		client.target(REST_SERVICE_URL).path("/{bookId}")
+				.resolveTemplate("bookId", bookId).request().delete();
+		List<Book> books2 = client.target(REST_SERVICE_URL).request()
+				.get(bookType);
+		assertThat(books2.isEmpty(), equalTo(true));
+	}
+}

entity/src/main/java/com/hascode/tutorial/jaxrs/entity/Book.java

 import java.math.BigDecimal;
 import java.util.Calendar;
 
-import javax.xml.bind.annotation.XmlRootElement;
-
-@XmlRootElement
 public class Book implements Serializable {
 	private static final long serialVersionUID = 1L;
 
 		this.published = published;
 	}
 
+	@Override
+	public String toString() {
+		return String.format("Book [id=%s, title=%s, price=%s, published=%s]",
+				id, title, price, published);
+	}
+
 }
 	<artifactId>rest-server</artifactId>
 	<packaging>war</packaging>
 
+	<properties>
+		<jackson.version>2.3.0</jackson.version>
+	</properties>
+
 	<dependencies>
 		<dependency>
 			<groupId>com.hascode.tutorial.rest</groupId>
 			<artifactId>entity</artifactId>
 			<version>1.0.0-SNAPSHOT</version>
 		</dependency>
+		<dependency>
+			<groupId>com.fasterxml.jackson.core</groupId>
+			<artifactId>jackson-core</artifactId>
+			<version>${jackson.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>com.fasterxml.jackson.core</groupId>
+			<artifactId>jackson-annotations</artifactId>
+			<version>${jackson.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>com.fasterxml.jackson.core</groupId>
+			<artifactId>jackson-databind</artifactId>
+			<version>${jackson.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>com.fasterxml.jackson.jaxrs</groupId>
+			<artifactId>jackson-jaxrs-json-provider</artifactId>
+			<version>${jackson.version}</version>
+		</dependency>
 	</dependencies>
 
 	<build>

server/src/main/java/com/hascode/tutorial/jaxrs/server/BookBean.java

 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 
 import javax.ejb.Singleton;
 import javax.ejb.Startup;
 	private final Map<String, Book> books = new HashMap<>();
 
 	@Override
-	public void saveBook(final Book book) {
+	public Book saveBook(final Book book) {
+		book.setId(UUID.randomUUID().toString());
 		books.put(book.getId(), book);
+		return book;
 	}
 
 	@Override
 	public List<Book> getAll() {
 		return new ArrayList<Book>(books.values());
 	}
+
+	@Override
+	public Book getById(final String id) {
+		return books.get(id);
+	}
 }

server/src/main/java/com/hascode/tutorial/jaxrs/server/BookRepository.java

 
 public interface BookRepository {
 
-	public abstract void saveBook(final Book book);
+	Book saveBook(final Book book);
 
-	public abstract void deleteBook(final String id);
+	void deleteBook(final String id);
 
-	public abstract List<Book> getAll();
+	List<Book> getAll();
+
+	Book getById(String id);
 
 }

server/src/main/java/com/hascode/tutorial/jaxrs/server/BookStoreService.java

 
 	@POST
 	@Consumes(MediaType.APPLICATION_JSON)
+	@Produces(MediaType.APPLICATION_JSON)
 	public Response saveBook(final Book book) {
-		bookRepository.saveBook(book);
-		return Response.ok().build();
+		Book bookPersisted = bookRepository.saveBook(book);
+		return Response.ok(bookPersisted).build();
 	}
 
 	@DELETE
 		};
 		return Response.ok(bookWrapper).build();
 	}
+
+	@GET
+	@Path("/{id}")
+	@Produces(MediaType.APPLICATION_JSON)
+	public Response getById(final @PathParam("id") String id) {
+		Book book = bookRepository.getById(id);
+		return Response.ok(book).build();
+	}
 }

server/src/main/resources/META-INF/services/jersey-server-components

+com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
+com.fasterxml.jackson.jaxrs.json.JsonMappingExceptionMapper
+com.fasterxml.jackson.jaxrs.json.JsonParseExceptionMapper