Skip to content

Commit cbd1a9d

Browse files
magkrausemaibin
authored andcommitted
Bael 1277 (eugenp#3379)
* BAEL-1277: RESTFul CRUD application with JavaLite. * BAEL-1277: RESTFul CRUD application with JavaLite. Adding exception handling. * BAEL-1277: Changes after editors review.
1 parent 727554b commit cbd1a9d

File tree

5 files changed

+79
-86
lines changed

5 files changed

+79
-86
lines changed

java-lite/pom.xml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<groupId>org.baeldung</groupId>
88
<artifactId>java-lite</artifactId>
99
<version>1.0-SNAPSHOT</version>
10+
<packaging>war</packaging>
1011

1112
<parent>
1213
<groupId>com.baeldung</groupId>
@@ -15,7 +16,7 @@
1516
</parent>
1617

1718
<properties>
18-
<jetty.maven.plugin.version>9.3.4.RC1</jetty.maven.plugin.version>
19+
<jetty.maven.plugin.version>9.4.8.v20171121</jetty.maven.plugin.version>
1920
<activejdbc.version>1.4.13</activejdbc.version>
2021
<activeweb.version>1.15</activeweb.version>
2122
<mysql.connector.java.version>5.1.45</mysql.connector.java.version>
@@ -85,16 +86,6 @@
8586
<systemPath>${java.home}/../lib/tools.jar</systemPath>
8687
</dependency>
8788

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>
9889
<dependency>
9990
<groupId>junit</groupId>
10091
<artifactId>junit</artifactId>

java-lite/src/main/java/app/config/AppControllerConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class AppControllerConfig extends AbstractControllerConfig {
1010
@Override
1111
public void init(AppContext appContext) {
12-
addGlobalFilters(new TimingFilter());
13-
add(new DBConnectionFilter()).to(ProductsController.class);
12+
addGlobalFilters(new TimingFilter());
13+
add(new DBConnectionFilter()).to(ProductsController.class);
1414
}
1515
}

java-lite/src/main/java/app/config/DbConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
public class DbConfig extends AbstractDBConfig {
77
@Override
88
public void init(AppContext appContext) {
9-
this.configFile("/database.properties");
9+
this.configFile("/database.properties");
1010
}
1111
}

java-lite/src/main/java/app/controllers/ProductsController.java

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,92 +10,94 @@
1010
@RESTful
1111
public class ProductsController extends AppController {
1212

13+
private ObjectMapper mapper = new ObjectMapper();
14+
1315
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-
}
16+
try {
17+
view("products", Product.findAll());
18+
render().contentType("application/json");
19+
} catch (Exception e) {
20+
view("message", "There was an error.", "code", 200);
21+
render("message");
22+
}
2123
}
2224

2325
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-
}
26+
try {
27+
Map payload = mapper.readValue(getRequestString(), Map.class);
28+
Product p = new Product();
29+
p.fromMap(payload);
30+
p.saveIt();
31+
view("message", "Successfully saved product id " + p.get("id"), "code", 200);
32+
render("message");
33+
} catch (Exception e) {
34+
view("message", "There was an error.", "code", 200);
35+
render("message");
36+
}
3537
}
3638

3739
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;
40+
try {
41+
Map payload = mapper.readValue(getRequestString(), Map.class);
42+
String id = getId();
43+
Product p = Product.findById(id);
44+
if (p == null) {
45+
view("message", "Product id " + id + " not found.", "code", 200);
46+
render("message");
47+
return;
48+
}
49+
p.fromMap(payload);
50+
p.saveIt();
51+
view("message", "Successfully updated product id " + id, "code", 200);
52+
render("message");
53+
} catch (Exception e) {
54+
view("message", "There was an error.", "code", 200);
55+
render("message");
4656
}
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-
}
5557
}
5658

5759
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;
60+
try {
61+
String id = getId();
62+
Product p = Product.findById(id);
63+
if (p == null) {
64+
view("message", "Product id " + id + " not found.", "code", 200);
65+
render("message");
66+
return;
67+
}
68+
view("product", p);
69+
render("_product");
70+
} catch (Exception e) {
71+
view("message", "There was an error.", "code", 200);
72+
render("message");
6573
}
66-
view("product", p);
67-
render("_product");
68-
} catch (Exception e) {
69-
view("message", "There was an error.", "code", 200);
70-
render("message");
71-
}
7274
}
7375

7476
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;
77+
try {
78+
String id = getId();
79+
Product p = Product.findById(id);
80+
if (p == null) {
81+
view("message", "Product id " + id + " not found.", "code", 200);
82+
render("message");
83+
return;
84+
}
85+
p.delete();
86+
view("message", "Successfully deleted product id " + id, "code", 200);
87+
render("message");
88+
} catch (Exception e) {
89+
view("message", "There was an error.", "code", 200);
90+
render("message");
8291
}
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-
}
9092
}
9193

9294
@Override
9395
protected String getContentType() {
94-
return "application/json";
96+
return "application/json";
9597
}
9698

9799
@Override
98100
protected String getLayout() {
99-
return null;
101+
return null;
100102
}
101103
}

java-lite/src/test/java/app/models/ProductTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ public class ProductTest {
88

99
//@Test
1010
public void givenSavedProduct_WhenFindFirst_ThenSavedProductIsReturned() {
11-
//Open DB connection
12-
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password");
11+
//Open DB connection
12+
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password");
1313

14-
//Create a product and save it
15-
Product toSaveProduct = new Product();
16-
toSaveProduct.set("name", "Bread");
17-
toSaveProduct.saveIt();
14+
//Create a product and save it
15+
Product toSaveProduct = new Product();
16+
toSaveProduct.set("name", "Bread");
17+
toSaveProduct.saveIt();
1818

19-
//Find product
20-
Product savedProduct = Product.findFirst("name = ?", "Bread");
19+
//Find product
20+
Product savedProduct = Product.findFirst("name = ?", "Bread");
2121

22-
Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name"));
22+
Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name"));
2323
}
2424

2525
}

0 commit comments

Comments
 (0)