Skip to content

Commit 1273bb4

Browse files
magkrausemaibin
authored andcommitted
BAEL-1277: RESTFul CRUD application with JavaLite (eugenp#3359)
* BAEL-1277: RESTFul CRUD application with JavaLite. * BAEL-1277: RESTFul CRUD application with JavaLite. Adding exception handling.
1 parent 749533e commit 1273bb4

15 files changed

Lines changed: 315 additions & 0 deletions

File tree

java-lite/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Relevant Articles:
2+
- [RESTFul CRUD application with JavaLite] ()

java-lite/pom.xml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.baeldung</groupId>
8+
<artifactId>java-lite</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<properties>
18+
<jetty.maven.plugin.version>9.3.4.RC1</jetty.maven.plugin.version>
19+
<activejdbc.version>1.4.13</activejdbc.version>
20+
<activeweb.version>1.15</activeweb.version>
21+
<mysql.connector.java.version>5.1.45</mysql.connector.java.version>
22+
<sun.tools.version>1.7.0</sun.tools.version>
23+
<jackson.version>1.8.2</jackson.version>
24+
<junit.version>4.11</junit.version>
25+
</properties>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.eclipse.jetty</groupId>
31+
<artifactId>jetty-maven-plugin</artifactId>
32+
<version>${jetty.maven.plugin.version}</version>
33+
<configuration>
34+
<systemProperties>
35+
<systemProperty>
36+
<name>activejdbc.log</name>
37+
<value></value>
38+
</systemProperty>
39+
<systemProperty>
40+
<name>active_reload</name>
41+
<value>true</value>
42+
</systemProperty>
43+
<systemProperty>
44+
<name>activeweb.log.request</name>
45+
<value>true</value>
46+
</systemProperty>
47+
</systemProperties>
48+
</configuration>
49+
</plugin>
50+
51+
<plugin>
52+
<groupId>org.javalite</groupId>
53+
<artifactId>activejdbc-instrumentation</artifactId>
54+
<version>${activejdbc.version}</version>
55+
<executions>
56+
<execution>
57+
<phase>process-classes</phase>
58+
<goals>
59+
<goal>instrument</goal>
60+
</goals>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
67+
<dependencies>
68+
<dependency>
69+
<groupId>org.javalite</groupId>
70+
<artifactId>activeweb</artifactId>
71+
<version>${activeweb.version}</version>
72+
</dependency>
73+
74+
<dependency>
75+
<groupId>mysql</groupId>
76+
<artifactId>mysql-connector-java</artifactId>
77+
<version>${mysql.connector.java.version}</version>
78+
</dependency>
79+
80+
<dependency>
81+
<groupId>com.sun</groupId>
82+
<artifactId>tools</artifactId>
83+
<version>${sun.tools.version}</version>
84+
<scope>system</scope>
85+
<systemPath>${java.home}/../lib/tools.jar</systemPath>
86+
</dependency>
87+
88+
<dependency>
89+
<groupId>org.codehaus.jackson</groupId>
90+
<artifactId>jackson-core-lgpl</artifactId>
91+
<version>${jackson.version}</version>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.codehaus.jackson</groupId>
95+
<artifactId>jackson-mapper-lgpl</artifactId>
96+
<version>${jackson.version}</version>
97+
</dependency>
98+
<dependency>
99+
<groupId>junit</groupId>
100+
<artifactId>junit</artifactId>
101+
<version>${junit.version}</version>
102+
</dependency>
103+
</dependencies>
104+
105+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package app.config;
2+
3+
import org.javalite.activeweb.AppContext;
4+
import org.javalite.activeweb.Bootstrap;
5+
6+
public class AppBootstrap extends Bootstrap {
7+
public void init(AppContext context) {
8+
}
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package app.config;
2+
3+
import app.controllers.ProductsController;
4+
import org.javalite.activeweb.AbstractControllerConfig;
5+
import org.javalite.activeweb.AppContext;
6+
import org.javalite.activeweb.controller_filters.DBConnectionFilter;
7+
import org.javalite.activeweb.controller_filters.TimingFilter;
8+
9+
public class AppControllerConfig extends AbstractControllerConfig {
10+
@Override
11+
public void init(AppContext appContext) {
12+
addGlobalFilters(new TimingFilter());
13+
add(new DBConnectionFilter()).to(ProductsController.class);
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package app.config;
2+
3+
import org.javalite.activeweb.AbstractDBConfig;
4+
import org.javalite.activeweb.AppContext;
5+
6+
public class DbConfig extends AbstractDBConfig {
7+
@Override
8+
public void init(AppContext appContext) {
9+
this.configFile("/database.properties");
10+
}
11+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package app.controllers;
2+
3+
import app.models.Product;
4+
import org.codehaus.jackson.map.ObjectMapper;
5+
import org.javalite.activeweb.AppController;
6+
import org.javalite.activeweb.annotations.RESTful;
7+
8+
import java.util.Map;
9+
10+
@RESTful
11+
public class ProductsController extends AppController {
12+
13+
public void index() {
14+
try {
15+
view("products", Product.findAll());
16+
render().contentType("application/json");
17+
} catch (Exception e) {
18+
view("message", "There was an error.", "code", 200);
19+
render("message");
20+
}
21+
}
22+
23+
public void create() {
24+
try {
25+
Map payload = new ObjectMapper().readValue(getRequestString(), Map.class);
26+
Product p = new Product();
27+
p.fromMap(payload);
28+
p.saveIt();
29+
view("message", "Successfully saved product id " + p.get("id"), "code", 200);
30+
render("message");
31+
} catch (Exception e) {
32+
view("message", "There was an error.", "code", 200);
33+
render("message");
34+
}
35+
}
36+
37+
public void update() {
38+
try {
39+
Map payload = new ObjectMapper().readValue(getRequestString(), Map.class);
40+
String id = getId();
41+
Product p = Product.findById(id);
42+
if (p == null) {
43+
view("message", "Product id " + id + " not found.", "code", 200);
44+
render("message");
45+
return;
46+
}
47+
p.fromMap(payload);
48+
p.saveIt();
49+
view("message", "Successfully updated product id " + id, "code", 200);
50+
render("message");
51+
} catch (Exception e) {
52+
view("message", "There was an error.", "code", 200);
53+
render("message");
54+
}
55+
}
56+
57+
public void show() {
58+
try {
59+
String id = getId();
60+
Product p = Product.findById(id);
61+
if (p == null) {
62+
view("message", "Product id " + id + " not found.", "code", 200);
63+
render("message");
64+
return;
65+
}
66+
view("product", p);
67+
render("_product");
68+
} catch (Exception e) {
69+
view("message", "There was an error.", "code", 200);
70+
render("message");
71+
}
72+
}
73+
74+
public void destroy() {
75+
try {
76+
String id = getId();
77+
Product p = Product.findById(id);
78+
if (p == null) {
79+
view("message", "Product id " + id + " not found.", "code", 200);
80+
render("message");
81+
return;
82+
}
83+
p.delete();
84+
view("message", "Successfully deleted product id " + id, "code", 200);
85+
render("message");
86+
} catch (Exception e) {
87+
view("message", "There was an error.", "code", 200);
88+
render("message");
89+
}
90+
}
91+
92+
@Override
93+
protected String getContentType() {
94+
return "application/json";
95+
}
96+
97+
@Override
98+
protected String getLayout() {
99+
return null;
100+
}
101+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package app.models;
2+
3+
import org.javalite.activejdbc.Model;
4+
5+
public class Product extends Model {
6+
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
development.driver=com.mysql.jdbc.Driver
2+
development.username=user
3+
development.password=password
4+
development.url=jdbc:mysql://localhost/dbname
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id" : ${product.id},
3+
"name" : "${product.name}"
4+
}

0 commit comments

Comments
 (0)