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>
|
<groupId>org.baeldung</groupId>
|
||||||
<artifactId>java-lite</artifactId>
|
<artifactId>java-lite</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
@ -15,7 +16,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<properties>
|
<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>
|
<activejdbc.version>1.4.13</activejdbc.version>
|
||||||
<activeweb.version>1.15</activeweb.version>
|
<activeweb.version>1.15</activeweb.version>
|
||||||
<mysql.connector.java.version>5.1.45</mysql.connector.java.version>
|
<mysql.connector.java.version>5.1.45</mysql.connector.java.version>
|
||||||
|
@ -85,16 +86,6 @@
|
||||||
<systemPath>${java.home}/../lib/tools.jar</systemPath>
|
<systemPath>${java.home}/../lib/tools.jar</systemPath>
|
||||||
</dependency>
|
</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>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
|
|
@ -9,7 +9,7 @@ import org.javalite.activeweb.controller_filters.TimingFilter;
|
||||||
public class AppControllerConfig extends AbstractControllerConfig {
|
public class AppControllerConfig extends AbstractControllerConfig {
|
||||||
@Override
|
@Override
|
||||||
public void init(AppContext appContext) {
|
public void init(AppContext appContext) {
|
||||||
addGlobalFilters(new TimingFilter());
|
addGlobalFilters(new TimingFilter());
|
||||||
add(new DBConnectionFilter()).to(ProductsController.class);
|
add(new DBConnectionFilter()).to(ProductsController.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,6 @@ import org.javalite.activeweb.AppContext;
|
||||||
public class DbConfig extends AbstractDBConfig {
|
public class DbConfig extends AbstractDBConfig {
|
||||||
@Override
|
@Override
|
||||||
public void init(AppContext appContext) {
|
public void init(AppContext appContext) {
|
||||||
this.configFile("/database.properties");
|
this.configFile("/database.properties");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,92 +10,94 @@ import java.util.Map;
|
||||||
@RESTful
|
@RESTful
|
||||||
public class ProductsController extends AppController {
|
public class ProductsController extends AppController {
|
||||||
|
|
||||||
|
private ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
public void index() {
|
public void index() {
|
||||||
try {
|
try {
|
||||||
view("products", Product.findAll());
|
view("products", Product.findAll());
|
||||||
render().contentType("application/json");
|
render().contentType("application/json");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
view("message", "There was an error.", "code", 200);
|
view("message", "There was an error.", "code", 200);
|
||||||
render("message");
|
render("message");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void create() {
|
public void create() {
|
||||||
try {
|
try {
|
||||||
Map payload = new ObjectMapper().readValue(getRequestString(), Map.class);
|
Map payload = mapper.readValue(getRequestString(), Map.class);
|
||||||
Product p = new Product();
|
Product p = new Product();
|
||||||
p.fromMap(payload);
|
p.fromMap(payload);
|
||||||
p.saveIt();
|
p.saveIt();
|
||||||
view("message", "Successfully saved product id " + p.get("id"), "code", 200);
|
view("message", "Successfully saved product id " + p.get("id"), "code", 200);
|
||||||
render("message");
|
render("message");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
view("message", "There was an error.", "code", 200);
|
view("message", "There was an error.", "code", 200);
|
||||||
render("message");
|
render("message");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
try {
|
try {
|
||||||
Map payload = new ObjectMapper().readValue(getRequestString(), Map.class);
|
Map payload = mapper.readValue(getRequestString(), Map.class);
|
||||||
String id = getId();
|
String id = getId();
|
||||||
Product p = Product.findById(id);
|
Product p = Product.findById(id);
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
view("message", "Product id " + id + " not found.", "code", 200);
|
view("message", "Product id " + id + " not found.", "code", 200);
|
||||||
render("message");
|
render("message");
|
||||||
return;
|
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() {
|
public void show() {
|
||||||
try {
|
try {
|
||||||
String id = getId();
|
String id = getId();
|
||||||
Product p = Product.findById(id);
|
Product p = Product.findById(id);
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
view("message", "Product id " + id + " not found.", "code", 200);
|
view("message", "Product id " + id + " not found.", "code", 200);
|
||||||
render("message");
|
render("message");
|
||||||
return;
|
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() {
|
public void destroy() {
|
||||||
try {
|
try {
|
||||||
String id = getId();
|
String id = getId();
|
||||||
Product p = Product.findById(id);
|
Product p = Product.findById(id);
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
view("message", "Product id " + id + " not found.", "code", 200);
|
view("message", "Product id " + id + " not found.", "code", 200);
|
||||||
render("message");
|
render("message");
|
||||||
return;
|
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
|
@Override
|
||||||
protected String getContentType() {
|
protected String getContentType() {
|
||||||
return "application/json";
|
return "application/json";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getLayout() {
|
protected String getLayout() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,18 +8,18 @@ public class ProductTest {
|
||||||
|
|
||||||
//@Test
|
//@Test
|
||||||
public void givenSavedProduct_WhenFindFirst_ThenSavedProductIsReturned() {
|
public void givenSavedProduct_WhenFindFirst_ThenSavedProductIsReturned() {
|
||||||
//Open DB connection
|
//Open DB connection
|
||||||
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password");
|
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password");
|
||||||
|
|
||||||
//Create a product and save it
|
//Create a product and save it
|
||||||
Product toSaveProduct = new Product();
|
Product toSaveProduct = new Product();
|
||||||
toSaveProduct.set("name", "Bread");
|
toSaveProduct.set("name", "Bread");
|
||||||
toSaveProduct.saveIt();
|
toSaveProduct.saveIt();
|
||||||
|
|
||||||
//Find product
|
//Find product
|
||||||
Product savedProduct = Product.findFirst("name = ?", "Bread");
|
Product savedProduct = Product.findFirst("name = ?", "Bread");
|
||||||
|
|
||||||
Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name"));
|
Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue