feat(rest-api): initial commit: NOT working
This commit is contained in:
parent
1d0b3eaa9e
commit
d03f366ac6
|
@ -0,0 +1,31 @@
|
||||||
|
|
||||||
|
# Files
|
||||||
|
.classpath
|
||||||
|
.externalToolBuilders
|
||||||
|
.gradle
|
||||||
|
.project
|
||||||
|
.pydevproject
|
||||||
|
.settings
|
||||||
|
.sonar
|
||||||
|
*~
|
||||||
|
*.ipr
|
||||||
|
*.iml
|
||||||
|
*.iws
|
||||||
|
*.swp
|
||||||
|
*.DS_Store
|
||||||
|
*.snap.debug
|
||||||
|
dependency-reduced-pom.xml
|
||||||
|
|
||||||
|
# Directories
|
||||||
|
.idea/
|
||||||
|
.run/
|
||||||
|
.venv/
|
||||||
|
_site/
|
||||||
|
build/
|
||||||
|
dist/
|
||||||
|
docs/_build/
|
||||||
|
gen-java/
|
||||||
|
gen-javabean/
|
||||||
|
gen-py/
|
||||||
|
node_modules/
|
||||||
|
target/
|
|
@ -0,0 +1,6 @@
|
||||||
|
# Overview
|
||||||
|
This project can serve as starting point for a REST API.
|
||||||
|
|
||||||
|
# Links
|
||||||
|
|
||||||
|
[Creating a REST API](https://happycoding.io/tutorials/java-server/rest-api)
|
|
@ -0,0 +1,101 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<name>rest_api</name>
|
||||||
|
<description>description</description>
|
||||||
|
<url>https://swingbe.de</url>
|
||||||
|
<groupId>de.swingbe</groupId>
|
||||||
|
<artifactId>rest_api</artifactId>
|
||||||
|
<version>0.0.1</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<licenses>
|
||||||
|
<license>
|
||||||
|
<name>GNU General Public License</name>
|
||||||
|
<url>https://www.gnu.org/licenses/gpl-3.0.txt</url>
|
||||||
|
</license>
|
||||||
|
</licenses>
|
||||||
|
|
||||||
|
<scm>
|
||||||
|
<url>https://github.com/Software-Ingenieur-Begerad/sandbox-java</url>
|
||||||
|
</scm>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- Other properties -->
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies><!-- https://mvnrepository.com/artifact/org.json/json -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>20220320</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<version>4.0.1</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.10.1</version>
|
||||||
|
<configuration>
|
||||||
|
<!-- Target Java versions -->
|
||||||
|
<release>11</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.4</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<filters>
|
||||||
|
<filter>
|
||||||
|
<!-- exclude signatures from merged JAR to avoid invalid signature messages -->
|
||||||
|
<artifact>*:*</artifact>
|
||||||
|
<excludes>
|
||||||
|
<exclude>META-INF/*.SF</exclude>
|
||||||
|
<exclude>META-INF/*.DSA</exclude>
|
||||||
|
<exclude>META-INF/*.RSA</exclude>
|
||||||
|
</excludes>
|
||||||
|
</filter>
|
||||||
|
</filters>
|
||||||
|
<!-- The shaded JAR will not be the main artifact for the project, it will be attached
|
||||||
|
for deployment in the way source and docs are. -->
|
||||||
|
<shadedArtifactAttached>true
|
||||||
|
</shadedArtifactAttached>
|
||||||
|
<shadedClassifierName>shaded</shadedClassifierName>
|
||||||
|
<transformers>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<manifestEntries>
|
||||||
|
<Main-Class>de.swingbe.rest_api.Main
|
||||||
|
</Main-Class>
|
||||||
|
</manifestEntries>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,9 @@
|
||||||
|
package de.swingbe.rest_api;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Hello world!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package de.swingbe.rest_api.controller;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import de.swingbe.rest_api.model.Person;
|
||||||
|
import de.swingbe.rest_api.storage.DataStore;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
public class PersonServlet extends HttpServlet {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||||
|
|
||||||
|
String requestUrl = request.getRequestURI();
|
||||||
|
String name = requestUrl.substring("/people/".length());
|
||||||
|
|
||||||
|
Person person = DataStore.getInstance().getPerson(name);
|
||||||
|
|
||||||
|
if (person != null) {
|
||||||
|
String json = "{\n";
|
||||||
|
json += "\"name\": " + JSONObject.quote(person.getName()) + ",\n";
|
||||||
|
json += "\"about\": " + JSONObject.quote(person.getAbout()) + ",\n";
|
||||||
|
json += "\"birthYear\": " + person.getBirthYear() + "\n";
|
||||||
|
json += "}";
|
||||||
|
response.getOutputStream().println(json);
|
||||||
|
} else {
|
||||||
|
//That person wasn't found, so return an empty JSON object. We could also return an error.
|
||||||
|
response.getOutputStream().println("{}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||||
|
|
||||||
|
String name = request.getParameter("name");
|
||||||
|
String about = request.getParameter("about");
|
||||||
|
int birthYear = Integer.parseInt(request.getParameter("birthYear"));
|
||||||
|
|
||||||
|
DataStore.getInstance().putPerson(new Person(name, about, birthYear));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package de.swingbe.rest_api.model;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
private final String name;
|
||||||
|
private final String about;
|
||||||
|
private final int birthYear;
|
||||||
|
|
||||||
|
public Person(String name, String about, int birthYear) {
|
||||||
|
this.name = name;
|
||||||
|
this.about = about;
|
||||||
|
this.birthYear = birthYear;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAbout() {
|
||||||
|
return about;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getBirthYear() {
|
||||||
|
return birthYear;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package de.swingbe.rest_api.storage;
|
||||||
|
|
||||||
|
import de.swingbe.rest_api.model.Person;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example DataStore class that provides access to user data.
|
||||||
|
* Pretend this class accesses a database.
|
||||||
|
*/
|
||||||
|
public class DataStore {
|
||||||
|
|
||||||
|
//this class is a singleton and should not be instantiated directly!
|
||||||
|
private static final DataStore instance = new DataStore();
|
||||||
|
//Map of names to Person instances.
|
||||||
|
private final Map<String, Person> personMap = new HashMap<>();
|
||||||
|
|
||||||
|
//private constructor so people know to use the getInstance() function instead
|
||||||
|
private DataStore() {
|
||||||
|
//dummy data
|
||||||
|
personMap.put("Ada", new Person("Ada", "Ada Lovelace was the first programmer.", 1815));
|
||||||
|
personMap.put("Kevin", new Person("Kevin", "Kevin is the author of HappyCoding.io.", 1986));
|
||||||
|
personMap.put("Stanley", new Person("Stanley", "Stanley is Kevin's cat.", 2007));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DataStore getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person getPerson(String name) {
|
||||||
|
return personMap.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putPerson(Person person) {
|
||||||
|
personMap.put(person.getName(), person);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
<web-app
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||||
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||||
|
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||||
|
version="3.1">
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>PersonServlet</servlet-name>
|
||||||
|
<servlet-class>PersonServlet</servlet-class>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>PersonServlet</servlet-name>
|
||||||
|
<url-pattern>/people/*</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
</web-app>
|
Loading…
Reference in New Issue