[JAVA-13854] Half list moved (#12598)

* [JAVA-13854] added parent module

* [JAVA-13854] moved apache-tapestry(submodule) to web-modules(parent)

* [JAVA-13854] moved bootique(submodule) to web-modules(parent)

* [JAVA-13854] moved dropwizard(submodule) to web-modules(parent)

* [JAVA-13854] moved blade(submodule) to web-modules(parent)

* [JAVA-13854] moved java-lite(submodule) to web-modules(parent)

* [JAVA-13854] moved jooby(submodule) to web-modules(parent)

* [JAVA-13854] moved linkrest(submodule) to web-modules(parent)

* [JAVA-13854] moved ninja(submodule) to web-modules(parent)

* [JAVA-13854] moved ratpack(submodule) to web-modules(parent)

* [JAVA-13854] moved resteasy(submodule) to web-modules(parent)

* [JAVA-13854] moved restx(submodule) to web-modules(parent)

* [JAVA-13854] moved spark-java(submodule) to web-modules(parent)

* [JAVA-13854] moved vraptor(submodule) to web-modules(parent)

* [JAVA-13854] delete modules that were moved

* [JAVA-13854]

* [JAVA-13854]

* [JAVA-13854] delete ninja submodule  + moved raml(submodule) to web-modules(parent)

* [JAVA-13854] moved gwt(submodule) to web-modules(parent)

* [JAVA-13854] moved jakarta-ee(submodule) to web-modules(parent)

* [JAVA-13854] moved javax-servlets(submodule) to web-modules(parent)

* [JAVA-13854] moved javax-servlets-2(submodule) to web-modules(parent)

* [JAVA-13854] moved jee-7(submodule) to web-modules(parent)

* [JAVA-13854] moved play-framework(not a module) to web-modules

* [JAVA-13854] fix failing test

* [JAVA-13854] moved struts-2(submodule) to web-modules(parent)

* [JAVA-13854] moved wicket(submodule) to web-modules(parent)

* [JAVA-13854] deleted modules that were moved to web-modules

* JAVA-13854 Removed moved modules from the main pom.xml

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
This commit is contained in:
panos-kakos
2022-08-21 10:14:00 +01:00
committed by GitHub
parent c56d1dc83d
commit 02066cab2c
755 changed files with 768 additions and 755 deletions

View File

@@ -0,0 +1,6 @@
## Spark Java
This module contains articles about Spark
### Relevant Articles
- [Building an API With the Spark Java Framework](https://www.baeldung.com/spark-framework-rest-api)

View File

@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spark-java</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>spark-java</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>web-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>${sparkjava.spark-core.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${google.code.gson.version}</version>
</dependency>
</dependencies>
<properties>
<sparkjava.spark-core.version>2.5.4</sparkjava.spark-core.version>
<google.code.gson.version>2.8.0</google.code.gson.version>
</properties>
</project>

View File

@@ -0,0 +1,13 @@
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");
});
}
}

View File

@@ -0,0 +1,64 @@
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"));
});
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,16 @@
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;
}
}

View File

@@ -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();
}
}

View File

@@ -0,0 +1,13 @@
package com.baeldung.sparkjava;
public class UserException extends Exception {
public UserException() {
super();
}
public UserException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,17 @@
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);
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>