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:
9
java-lite/src/main/java/app/config/AppBootstrap.java
Normal file
9
java-lite/src/main/java/app/config/AppBootstrap.java
Normal 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) {
|
||||
}
|
||||
}
|
||||
15
java-lite/src/main/java/app/config/AppControllerConfig.java
Normal file
15
java-lite/src/main/java/app/config/AppControllerConfig.java
Normal 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);
|
||||
}
|
||||
}
|
||||
11
java-lite/src/main/java/app/config/DbConfig.java
Normal file
11
java-lite/src/main/java/app/config/DbConfig.java
Normal 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");
|
||||
}
|
||||
}
|
||||
101
java-lite/src/main/java/app/controllers/ProductsController.java
Normal file
101
java-lite/src/main/java/app/controllers/ProductsController.java
Normal 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;
|
||||
}
|
||||
}
|
||||
7
java-lite/src/main/java/app/models/Product.java
Normal file
7
java-lite/src/main/java/app/models/Product.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package app.models;
|
||||
|
||||
import org.javalite.activejdbc.Model;
|
||||
|
||||
public class Product extends Model {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user