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:
parent
117635f955
commit
ad63b55edb
1
pom.xml
1
pom.xml
|
@ -94,6 +94,7 @@
|
|||
<module>resteasy</module>
|
||||
|
||||
<module>selenium-junit-testng</module>
|
||||
<module>spark-java</module>
|
||||
<module>spring-akka</module>
|
||||
<module>spring-amqp</module>
|
||||
<module>spring-all</module>
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
<?xml version="1.0"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spark-java</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>spark-java</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.sparkjava</groupId>
|
||||
<artifactId>spark-core</artifactId>
|
||||
<version>2.5.4</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.sparkjava;
|
||||
import static spark.Spark.*;
|
||||
|
||||
public class HelloWorldService {
|
||||
public static void main(String[] args) {
|
||||
get("/hello", (req,res)->"Hello, Baeldung");
|
||||
|
||||
get("/hello/:name", (req,res)->{
|
||||
return "Hello: "+ req.params(":name");
|
||||
});
|
||||
}
|
||||
}
|
|
@ -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" ));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.sparkjava;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
public class StandardResponse {
|
||||
private StatusResponse status;
|
||||
private String message;
|
||||
private JsonElement data;
|
||||
|
||||
public StandardResponse(StatusResponse status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public StandardResponse(StatusResponse status, String message) {
|
||||
this.status = status;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public StandardResponse(StatusResponse status, JsonElement data) {
|
||||
this.status = status;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public StatusResponse getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(StatusResponse status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public JsonElement getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(JsonElement data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.sparkjava;
|
||||
|
||||
public enum StatusResponse {
|
||||
SUCCESS ("Success"),
|
||||
ERROR ("Error");
|
||||
|
||||
final private String status;
|
||||
|
||||
StatusResponse(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.sparkjava;
|
||||
|
||||
class User {
|
||||
|
||||
private String id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String email;
|
||||
|
||||
public User(String id, String firstName, String lastName, String email) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringBuffer().append(getEmail()).toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.sparkjava;
|
||||
|
||||
public class UserException extends Exception{
|
||||
|
||||
public UserException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.sparkjava;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface UserService {
|
||||
public void addUser (User user) ;
|
||||
public Collection<User> getUsers () ;
|
||||
public User getUser (String id) ;
|
||||
public User editUser (User user) throws UserException;
|
||||
public void deleteUser (String id) ;
|
||||
public boolean userExist (String id);
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.sparkjava;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class UserServiceMapImpl implements UserService{
|
||||
private HashMap<String, User> userMap;
|
||||
|
||||
public UserServiceMapImpl() {
|
||||
userMap = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addUser (User user) {
|
||||
userMap.put(user.getId(), user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<User> getUsers () {
|
||||
return userMap.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getUser (String id) {
|
||||
return userMap.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User editUser (User forEdit) throws UserException{
|
||||
try{
|
||||
if (forEdit.getId() == null)
|
||||
throw new UserException("ID cannot be blank");
|
||||
|
||||
User toEdit = userMap.get(forEdit.getId());
|
||||
|
||||
if (toEdit == null )
|
||||
throw new UserException("User not found");
|
||||
|
||||
if (forEdit.getEmail()!=null) {
|
||||
toEdit.setEmail(forEdit.getEmail());
|
||||
}
|
||||
if (forEdit.getFirstName()!=null) {
|
||||
toEdit.setFirstName(forEdit.getFirstName());
|
||||
}
|
||||
if (forEdit.getLastName()!=null) {
|
||||
toEdit.setLastName(forEdit.getLastName());
|
||||
}
|
||||
if (forEdit.getId()!=null) {
|
||||
toEdit.setId(forEdit.getId());
|
||||
}
|
||||
|
||||
return toEdit;
|
||||
}catch (Exception ex) {
|
||||
throw new UserException(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteUser (String id) {
|
||||
userMap.remove(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean userExist (String id) {
|
||||
return userMap.containsKey(id);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue