Bael 1277 (#3379)
* BAEL-1277: RESTFul CRUD application with JavaLite. * BAEL-1277: RESTFul CRUD application with JavaLite. Adding exception handling. * BAEL-1277: Changes after editors review.
This commit is contained in:
parent
727554bf47
commit
cbd1a9dfbf
@ -7,6 +7,7 @@
|
||||
<groupId>org.baeldung</groupId>
|
||||
<artifactId>java-lite</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
@ -15,7 +16,7 @@
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<jetty.maven.plugin.version>9.3.4.RC1</jetty.maven.plugin.version>
|
||||
<jetty.maven.plugin.version>9.4.8.v20171121</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>
|
||||
@ -85,16 +86,6 @@
|
||||
<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>
|
||||
|
@ -9,7 +9,7 @@ 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);
|
||||
addGlobalFilters(new TimingFilter());
|
||||
add(new DBConnectionFilter()).to(ProductsController.class);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,6 @@ import org.javalite.activeweb.AppContext;
|
||||
public class DbConfig extends AbstractDBConfig {
|
||||
@Override
|
||||
public void init(AppContext appContext) {
|
||||
this.configFile("/database.properties");
|
||||
this.configFile("/database.properties");
|
||||
}
|
||||
}
|
||||
|
@ -10,92 +10,94 @@ import java.util.Map;
|
||||
@RESTful
|
||||
public class ProductsController extends AppController {
|
||||
|
||||
private ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
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");
|
||||
}
|
||||
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");
|
||||
}
|
||||
try {
|
||||
Map payload = mapper.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;
|
||||
try {
|
||||
Map payload = mapper.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");
|
||||
}
|
||||
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;
|
||||
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");
|
||||
}
|
||||
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;
|
||||
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");
|
||||
}
|
||||
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";
|
||||
return "application/json";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLayout() {
|
||||
return null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -8,18 +8,18 @@ public class ProductTest {
|
||||
|
||||
//@Test
|
||||
public void givenSavedProduct_WhenFindFirst_ThenSavedProductIsReturned() {
|
||||
//Open DB connection
|
||||
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password");
|
||||
//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();
|
||||
//Create a product and save it
|
||||
Product toSaveProduct = new Product();
|
||||
toSaveProduct.set("name", "Bread");
|
||||
toSaveProduct.saveIt();
|
||||
|
||||
//Find product
|
||||
Product savedProduct = Product.findFirst("name = ?", "Bread");
|
||||
//Find product
|
||||
Product savedProduct = Product.findFirst("name = ?", "Bread");
|
||||
|
||||
Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name"));
|
||||
Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name"));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user