Spark Java Article BAEL-498 (#912)
* Initial Commit for Spark Java Article BAEL-498 * reverting main pom.xml and rollbacking accidental changes. * Changes as per review: 1. Added UserService 2. Renamed UserStore to UserServiceMapImpl 3. Removed Empty spaces in User.java 4. Removed AppTest 5. Changes in SparkRestExample for using UserServiceMapImp instead of UserStore static functions. * Suggested changes in print messages. * Changes as per comments on github... for PR: https://github.com/eugenp/tutorials/pull/912 * Changes in editUser function as per guidance by Kevin. * Clean up * added 1.8 config for pom.xml * Clean up. * Removed junit dep. * Added Application/json in response type.
This commit is contained in:
committed by
KevinGilmore
parent
117635f955
commit
ad63b55edb
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.sparkjava;
|
||||
|
||||
import static spark.Spark.delete;
|
||||
import static spark.Spark.get;
|
||||
import static spark.Spark.options;
|
||||
import static spark.Spark.post;
|
||||
import static spark.Spark.put;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
public class SparkRestExample {
|
||||
public static void main(String[] args) {
|
||||
final UserService userService = new UserServiceMapImpl();
|
||||
|
||||
post("/users", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
User user = new Gson().fromJson(request.body(), User.class);
|
||||
userService.addUser(user);
|
||||
|
||||
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS));
|
||||
});
|
||||
|
||||
get("/users", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.SUCCESS,new Gson()
|
||||
.toJsonTree(userService.getUsers())));
|
||||
});
|
||||
|
||||
get("/users/:id", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.SUCCESS,new Gson()
|
||||
.toJsonTree(userService.getUser(request.params(":id")))));
|
||||
});
|
||||
|
||||
put("/users/:id", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
User toEdit = new Gson().fromJson(request.body(), User.class);
|
||||
User editedUser = userService.editUser(toEdit);
|
||||
|
||||
if (editedUser != null) {
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.SUCCESS,new Gson()
|
||||
.toJsonTree(editedUser)));
|
||||
}else {
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.ERROR,new Gson()
|
||||
.toJson("User not found or error in edit")));
|
||||
}
|
||||
});
|
||||
|
||||
delete("/users/:id", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
userService.deleteUser(request.params(":id"));
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.SUCCESS, "user deleted"));
|
||||
});
|
||||
|
||||
options("/users/:id", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.SUCCESS,
|
||||
(userService.userExist(
|
||||
request.params(":id"))) ? "User exists" : "User does not exists" ));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user