BAEL-1277: RESTFul CRUD application with JavaLite (#3359)

* BAEL-1277: RESTFul CRUD application with JavaLite.

* BAEL-1277: RESTFul CRUD application with JavaLite. Adding exception handling.
This commit is contained in:
Magdalena Krause 2018-01-05 16:51:47 -03:00 committed by maibin
parent 749533e14d
commit 1273bb4aee
15 changed files with 315 additions and 0 deletions

2
java-lite/README.md Normal file
View File

@ -0,0 +1,2 @@
### Relevant Articles:
- [RESTFul CRUD application with JavaLite] ()

105
java-lite/pom.xml Normal file
View File

@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>org.baeldung</groupId>
<artifactId>java-lite</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<jetty.maven.plugin.version>9.3.4.RC1</jetty.maven.plugin.version>
<activejdbc.version>1.4.13</activejdbc.version>
<activeweb.version>1.15</activeweb.version>
<mysql.connector.java.version>5.1.45</mysql.connector.java.version>
<sun.tools.version>1.7.0</sun.tools.version>
<jackson.version>1.8.2</jackson.version>
<junit.version>4.11</junit.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.maven.plugin.version}</version>
<configuration>
<systemProperties>
<systemProperty>
<name>activejdbc.log</name>
<value></value>
</systemProperty>
<systemProperty>
<name>active_reload</name>
<value>true</value>
</systemProperty>
<systemProperty>
<name>activeweb.log.request</name>
<value>true</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
<plugin>
<groupId>org.javalite</groupId>
<artifactId>activejdbc-instrumentation</artifactId>
<version>${activejdbc.version}</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.javalite</groupId>
<artifactId>activeweb</artifactId>
<version>${activeweb.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.java.version}</version>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>${sun.tools.version}</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,9 @@
package app.config;
import org.javalite.activeweb.AppContext;
import org.javalite.activeweb.Bootstrap;
public class AppBootstrap extends Bootstrap {
public void init(AppContext context) {
}
}

View File

@ -0,0 +1,15 @@
package app.config;
import app.controllers.ProductsController;
import org.javalite.activeweb.AbstractControllerConfig;
import org.javalite.activeweb.AppContext;
import org.javalite.activeweb.controller_filters.DBConnectionFilter;
import org.javalite.activeweb.controller_filters.TimingFilter;
public class AppControllerConfig extends AbstractControllerConfig {
@Override
public void init(AppContext appContext) {
addGlobalFilters(new TimingFilter());
add(new DBConnectionFilter()).to(ProductsController.class);
}
}

View File

@ -0,0 +1,11 @@
package app.config;
import org.javalite.activeweb.AbstractDBConfig;
import org.javalite.activeweb.AppContext;
public class DbConfig extends AbstractDBConfig {
@Override
public void init(AppContext appContext) {
this.configFile("/database.properties");
}
}

View File

@ -0,0 +1,101 @@
package app.controllers;
import app.models.Product;
import org.codehaus.jackson.map.ObjectMapper;
import org.javalite.activeweb.AppController;
import org.javalite.activeweb.annotations.RESTful;
import java.util.Map;
@RESTful
public class ProductsController extends AppController {
public void index() {
try {
view("products", Product.findAll());
render().contentType("application/json");
} catch (Exception e) {
view("message", "There was an error.", "code", 200);
render("message");
}
}
public void create() {
try {
Map payload = new ObjectMapper().readValue(getRequestString(), Map.class);
Product p = new Product();
p.fromMap(payload);
p.saveIt();
view("message", "Successfully saved product id " + p.get("id"), "code", 200);
render("message");
} catch (Exception e) {
view("message", "There was an error.", "code", 200);
render("message");
}
}
public void update() {
try {
Map payload = new ObjectMapper().readValue(getRequestString(), Map.class);
String id = getId();
Product p = Product.findById(id);
if (p == null) {
view("message", "Product id " + id + " not found.", "code", 200);
render("message");
return;
}
p.fromMap(payload);
p.saveIt();
view("message", "Successfully updated product id " + id, "code", 200);
render("message");
} catch (Exception e) {
view("message", "There was an error.", "code", 200);
render("message");
}
}
public void show() {
try {
String id = getId();
Product p = Product.findById(id);
if (p == null) {
view("message", "Product id " + id + " not found.", "code", 200);
render("message");
return;
}
view("product", p);
render("_product");
} catch (Exception e) {
view("message", "There was an error.", "code", 200);
render("message");
}
}
public void destroy() {
try {
String id = getId();
Product p = Product.findById(id);
if (p == null) {
view("message", "Product id " + id + " not found.", "code", 200);
render("message");
return;
}
p.delete();
view("message", "Successfully deleted product id " + id, "code", 200);
render("message");
} catch (Exception e) {
view("message", "There was an error.", "code", 200);
render("message");
}
}
@Override
protected String getContentType() {
return "application/json";
}
@Override
protected String getLayout() {
return null;
}
}

View File

@ -0,0 +1,7 @@
package app.models;
import org.javalite.activejdbc.Model;
public class Product extends Model {
}

View File

@ -0,0 +1,4 @@
development.driver=com.mysql.jdbc.Driver
development.username=user
development.password=password
development.url=jdbc:mysql://localhost/dbname

View File

@ -0,0 +1 @@
,

View File

@ -0,0 +1,4 @@
{
"id" : ${product.id},
"name" : "${product.name}"
}

View File

@ -0,0 +1 @@
[<@render partial="product" collection=products spacer="comma"/>]

View File

@ -0,0 +1,4 @@
{
"message" : "${message}",
"code" : ${code}
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>dispatcher</filter-name>
<filter-class>org.javalite.activeweb.RequestDispatcher</filter-class>
<init-param>
<param-name>exclusions</param-name>
<param-value>css,images,js,ico</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>dispatcher</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

View File

@ -0,0 +1,25 @@
package app.models;
import org.javalite.activejdbc.Base;
import org.junit.Assert;
import org.junit.Test;
public class ProductTest {
//@Test
public void givenSavedProduct_WhenFindFirst_ThenSavedProductIsReturned() {
//Open DB connection
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password");
//Create a product and save it
Product toSaveProduct = new Product();
toSaveProduct.set("name", "Bread");
toSaveProduct.saveIt();
//Find product
Product savedProduct = Product.findFirst("name = ?", "Bread");
Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name"));
}
}

View File

@ -85,6 +85,7 @@
<module>jackson</module> <module>jackson</module>
<!-- <module>persistence-modules/java-cassandra</module> --> <!-- <module>persistence-modules/java-cassandra</module> -->
<module>vavr</module> <module>vavr</module>
<module>java-lite</module>
<module>javax-servlets</module> <module>javax-servlets</module>
<module>javaxval</module> <module>javaxval</module>
<module>jaxb</module> <module>jaxb</module>