Skip to content

Commit ad63b55

Browse files
joshiparthinKevinGilmore
authored andcommitted
Spark Java Article BAEL-498 (eugenp#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: eugenp#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.
1 parent 117635f commit ad63b55

10 files changed

Lines changed: 334 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<module>resteasy</module>
9595

9696
<module>selenium-junit-testng</module>
97+
<module>spark-java</module>
9798
<module>spring-akka</module>
9899
<module>spring-amqp</module>
99100
<module>spring-all</module>

‎spark-java/pom.xml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>spark-java</artifactId>
8+
<version>0.1.0-SNAPSHOT</version>
9+
<name>spark-java</name>
10+
<url>http://maven.apache.org</url>
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.sparkjava</groupId>
19+
<artifactId>spark-core</artifactId>
20+
<version>2.5.4</version>
21+
</dependency>
22+
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
23+
<dependency>
24+
<groupId>com.google.code.gson</groupId>
25+
<artifactId>gson</artifactId>
26+
<version>2.8.0</version>
27+
</dependency>
28+
</dependencies>
29+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.sparkjava;
2+
import static spark.Spark.*;
3+
4+
public class HelloWorldService {
5+
public static void main(String[] args) {
6+
get("/hello", (req,res)->"Hello, Baeldung");
7+
8+
get("/hello/:name", (req,res)->{
9+
return "Hello: "+ req.params(":name");
10+
});
11+
}
12+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.sparkjava;
2+
3+
import static spark.Spark.delete;
4+
import static spark.Spark.get;
5+
import static spark.Spark.options;
6+
import static spark.Spark.post;
7+
import static spark.Spark.put;
8+
9+
import com.google.gson.Gson;
10+
11+
public class SparkRestExample {
12+
public static void main(String[] args) {
13+
final UserService userService = new UserServiceMapImpl();
14+
15+
post("/users", (request, response) -> {
16+
response.type("application/json");
17+
18+
User user = new Gson().fromJson(request.body(), User.class);
19+
userService.addUser(user);
20+
21+
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS));
22+
});
23+
24+
get("/users", (request, response) -> {
25+
response.type("application/json");
26+
27+
return new Gson().toJson(
28+
new StandardResponse(StatusResponse.SUCCESS,new Gson()
29+
.toJsonTree(userService.getUsers())));
30+
});
31+
32+
get("/users/:id", (request, response) -> {
33+
response.type("application/json");
34+
35+
return new Gson().toJson(
36+
new StandardResponse(StatusResponse.SUCCESS,new Gson()
37+
.toJsonTree(userService.getUser(request.params(":id")))));
38+
});
39+
40+
put("/users/:id", (request, response) -> {
41+
response.type("application/json");
42+
43+
User toEdit = new Gson().fromJson(request.body(), User.class);
44+
User editedUser = userService.editUser(toEdit);
45+
46+
if (editedUser != null) {
47+
return new Gson().toJson(
48+
new StandardResponse(StatusResponse.SUCCESS,new Gson()
49+
.toJsonTree(editedUser)));
50+
}else {
51+
return new Gson().toJson(
52+
new StandardResponse(StatusResponse.ERROR,new Gson()
53+
.toJson("User not found or error in edit")));
54+
}
55+
});
56+
57+
delete("/users/:id", (request, response) -> {
58+
response.type("application/json");
59+
60+
userService.deleteUser(request.params(":id"));
61+
return new Gson().toJson(
62+
new StandardResponse(StatusResponse.SUCCESS, "user deleted"));
63+
});
64+
65+
options("/users/:id", (request, response) -> {
66+
response.type("application/json");
67+
68+
return new Gson().toJson(
69+
new StandardResponse(StatusResponse.SUCCESS,
70+
(userService.userExist(
71+
request.params(":id"))) ? "User exists" : "User does not exists" ));
72+
});
73+
74+
}
75+
76+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.sparkjava;
2+
3+
import com.google.gson.JsonElement;
4+
5+
public class StandardResponse {
6+
private StatusResponse status;
7+
private String message;
8+
private JsonElement data;
9+
10+
public StandardResponse(StatusResponse status) {
11+
this.status = status;
12+
}
13+
14+
public StandardResponse(StatusResponse status, String message) {
15+
this.status = status;
16+
this.message = message;
17+
}
18+
19+
public StandardResponse(StatusResponse status, JsonElement data) {
20+
this.status = status;
21+
this.data = data;
22+
}
23+
24+
public StatusResponse getStatus() {
25+
return status;
26+
}
27+
28+
public void setStatus(StatusResponse status) {
29+
this.status = status;
30+
}
31+
32+
public String getMessage() {
33+
return message;
34+
}
35+
36+
public void setMessage(String message) {
37+
this.message = message;
38+
}
39+
40+
public JsonElement getData() {
41+
return data;
42+
}
43+
44+
public void setData(JsonElement data) {
45+
this.data = data;
46+
}
47+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.sparkjava;
2+
3+
public enum StatusResponse {
4+
SUCCESS ("Success"),
5+
ERROR ("Error");
6+
7+
final private String status;
8+
9+
StatusResponse(String status) {
10+
this.status = status;
11+
}
12+
13+
public String getStatus() {
14+
return status;
15+
}
16+
17+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.baeldung.sparkjava;
7+
8+
class User {
9+
10+
private String id;
11+
private String firstName;
12+
private String lastName;
13+
private String email;
14+
15+
public User(String id, String firstName, String lastName, String email) {
16+
super();
17+
this.id = id;
18+
this.firstName = firstName;
19+
this.lastName = lastName;
20+
this.email = email;
21+
}
22+
23+
public String getId() {
24+
return id;
25+
}
26+
27+
public void setId(String id) {
28+
this.id = id;
29+
}
30+
31+
public String getFirstName() {
32+
return firstName;
33+
}
34+
35+
public void setFirstName(String firstName) {
36+
this.firstName = firstName;
37+
}
38+
39+
public String getLastName() {
40+
return lastName;
41+
}
42+
43+
public void setLastName(String lastName) {
44+
this.lastName = lastName;
45+
}
46+
47+
public String getEmail() {
48+
return email;
49+
}
50+
51+
public void setEmail(String email) {
52+
this.email = email;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return new StringBuffer().append(getEmail()).toString();
58+
}
59+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.sparkjava;
2+
3+
public class UserException extends Exception{
4+
5+
public UserException() {
6+
super();
7+
}
8+
9+
public UserException(String message) {
10+
super(message);
11+
}
12+
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.sparkjava;
2+
3+
import java.util.Collection;
4+
5+
public interface UserService {
6+
public void addUser (User user) ;
7+
public Collection<User> getUsers () ;
8+
public User getUser (String id) ;
9+
public User editUser (User user) throws UserException;
10+
public void deleteUser (String id) ;
11+
public boolean userExist (String id);
12+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.sparkjava;
2+
3+
import java.util.Collection;
4+
import java.util.HashMap;
5+
6+
public class UserServiceMapImpl implements UserService{
7+
private HashMap<String, User> userMap;
8+
9+
public UserServiceMapImpl() {
10+
userMap = new HashMap<>();
11+
}
12+
13+
@Override
14+
public void addUser (User user) {
15+
userMap.put(user.getId(), user);
16+
}
17+
18+
@Override
19+
public Collection<User> getUsers () {
20+
return userMap.values();
21+
}
22+
23+
@Override
24+
public User getUser (String id) {
25+
return userMap.get(id);
26+
}
27+
28+
@Override
29+
public User editUser (User forEdit) throws UserException{
30+
try{
31+
if (forEdit.getId() == null)
32+
throw new UserException("ID cannot be blank");
33+
34+
User toEdit = userMap.get(forEdit.getId());
35+
36+
if (toEdit == null )
37+
throw new UserException("User not found");
38+
39+
if (forEdit.getEmail()!=null) {
40+
toEdit.setEmail(forEdit.getEmail());
41+
}
42+
if (forEdit.getFirstName()!=null) {
43+
toEdit.setFirstName(forEdit.getFirstName());
44+
}
45+
if (forEdit.getLastName()!=null) {
46+
toEdit.setLastName(forEdit.getLastName());
47+
}
48+
if (forEdit.getId()!=null) {
49+
toEdit.setId(forEdit.getId());
50+
}
51+
52+
return toEdit;
53+
}catch (Exception ex) {
54+
throw new UserException(ex.getMessage());
55+
}
56+
}
57+
58+
@Override
59+
public void deleteUser (String id) {
60+
userMap.remove(id);
61+
}
62+
63+
@Override
64+
public boolean userExist (String id) {
65+
return userMap.containsKey(id);
66+
}
67+
68+
}

0 commit comments

Comments
 (0)