Skip to content

Instantly share code, notes, and snippets.

@adben
Created September 10, 2013 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adben/6508674 to your computer and use it in GitHub Desktop.
Save adben/6508674 to your computer and use it in GitHub Desktop.
GSGNL
package com.github.adben.model;
public class Book {
private final double isbn;
private final long price;
private final String author;
private final String title;
private final String engTitle;
private final String imagePath;
public Book(BookBuilder builder) {
this.isbn = builder.isbn;
this.title = builder.title;
this.engTitle = builder.engTitle;
this.author = builder.author;
this.price = builder.price;
this.imagePath = builder.imagePath;
}
public String getEngTitle() {
return engTitle;
}
public double getIsbn() {
return isbn;
}
public long getPrice() {
return price;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getImagePath() {
return imagePath;
}
public static class BookBuilder {
private double isbn;
private long price;
private String author;
private String title;
private String engTitle;
private String imagePath;
public BookBuilder withIsbn(double i) {
this.isbn = i;
return this;
}
public BookBuilder withPrice(long p) {
this.price = p;
return this;
}
public BookBuilder withAuthor(String a) {
this.author = a;
return this;
}
public BookBuilder withTitle(String t) {
this.title = t;
return this;
}
public BookBuilder withEnglishTitle(String e) {
this.engTitle = e;
return this;
}
public BookBuilder withImagePath(String img) {
this.imagePath = img;
return this;
}
public Book build() {
return new Book(this);
}
}
}
package com.github.adben.yeomaven;
import com.github.adben.model.Book;
import com.google.common.collect.ImmutableSet;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import java.util.Collection;
@Path("/books")
public class BookResource {
public static final ImmutableSet<Book> COLOMBIAN_BOOKS = ImmutableSet.of(
new Book.BookBuilder().withIsbn(307474728)
.withAuthor("Gabriel Garcia Marquez")
.withImagePath("http://ecx.images-amazon.com/images/I/51fGhfrrU0L._SY346_PJlook-inside-v2,TopRight,1,0_SH20_.jpg")
.withTitle("Cien anos de soledad")
.withEnglishTitle("One Hundred Years of Solitude")
.withPrice(35).build(),
new Book.BookBuilder().withIsbn(297474780)
.withAuthor("Gabriel Garcia Marquez")
.withImagePath("http://ecx.images-amazon.com/images/I/51MVQeBxA8L._SY346_PJlook-inside-v2,TopRight,1,0_SH20_.jpg")
.withTitle("El amor en los tiempos del cólera")
.withEnglishTitle("Love in the Time of Cholera")
.withPrice(25).build(),
new Book.BookBuilder().withIsbn(567400728)
.withAuthor("Héctor Abad Faciolince")
.withImagePath("http://ecx.images-amazon.com/images/I/51EzVv%2BD%2BpL._SY346_PJlook-inside-v2,TopRight,1,0_SH20_.jpg")
.withTitle("El olvido que seremos")
.withEnglishTitle("We wil be…forgotten")
.withPrice(45).build(),
new Book.BookBuilder().withIsbn(677479887)
.withAuthor("Laura Restrepo")
.withImagePath("http://ecx.images-amazon.com/images/I/41y9ri-4GCL._PJlook-inside-v2,TopRight,1,0_SH20_.jpg")
.withTitle("Delirio")
.withEnglishTitle("Delirium")
.withPrice(30).build()
);
@GET
public Collection<Book> list() {
return COLOMBIAN_BOOKS;
}
}
.classpath
.idea
.project
.settings/
*.iml
target/
atlassian-ide-plugin.xml
mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2 -DgroupId=com.github.adben -DartifactId=yeomaven -Dversion=1.0.0-SNAPSHOT -Dpackage=com.github.adben
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.adben</groupId>
<artifactId>yeomaven</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>yeomaven</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<jersey-version>1.8</jersey-version>
<maven-clean-plugin.version>2.5</maven-clean-plugin.version>
<maven-war-plugin.version>2.3</maven-war-plugin.version>
<yeoman-maven-plugin.version>0.1</yeoman-maven-plugin.version>
<guava.version>14.0</guava.version>
<hamcrest-library.version>1.3</hamcrest-library.version>
<junit.version>4.11</junit.version>
</properties>
<build>
<finalName>yeomaven</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${hamcrest-library.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey-version}</version>
</dependency>
</dependencies>
</project>
package com.github.adben.model;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
* Test voor {@link com.github.adben.model.Book}
*/
public class BookTest {
private static final String AUTHOR = "Adolfo Benedetti";
private static final String TITLE = "Un Titulo";
private static final String ENG_TITLE = "A Title";
private static final String IMG_URL = "http://example.com/book.gif";
private static final double ISBN = 307474728.000;
private static final long PRICE = 30;
@Test
public void it_should_build_a_book_object() throws Exception {
Book testBook = new Book.BookBuilder()
.withAuthor(AUTHOR)
.withEnglishTitle(ENG_TITLE)
.withTitle(TITLE)
.withImagePath(IMG_URL)
.withIsbn(ISBN)
.withPrice(PRICE)
.build();
assertThat(testBook, notNullValue());
assertThat(testBook.getAuthor(), is(AUTHOR));
assertThat(testBook.getTitle(), is(TITLE));
assertThat(testBook.getEngTitle(), is(ENG_TITLE));
assertThat(testBook.getImagePath(), is(IMG_URL));
assertThat(testBook.getPrice(), is(PRICE));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.github.adben</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment