feat: add REST exercise
This commit is contained in:
parent
23a3e260ee
commit
bce2823d39
|
@ -0,0 +1,4 @@
|
|||
/target/
|
||||
.project
|
||||
.settings
|
||||
.classpath
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
run:
|
||||
mvn spring-boot:run
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>me.srvstr.rest</groupId>
|
||||
<artifactId>rest</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>rmi</name>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.release>17</maven.compiler.release>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
<version>3.4.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>3.4.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.11.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>me.srvstr.rest.Main</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,65 @@
|
|||
package me.srvstr.rest;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
|
||||
import me.srvstr.rest.model.Student;
|
||||
|
||||
@RestController
|
||||
public class Controller {
|
||||
|
||||
private static Map<Integer, Student> students = new HashMap<Integer, Student>();
|
||||
|
||||
@GetMapping("/api/v0/hello-world")
|
||||
public String HelloWorld() {
|
||||
return "Hello, World!";
|
||||
}
|
||||
|
||||
@GetMapping("/api/v0/student")
|
||||
public String getSomeStudent() {
|
||||
return new Gson().toJson(new Student(1, "Klaus Peter", 123456));
|
||||
}
|
||||
|
||||
@GetMapping("/api/v1/students")
|
||||
public String getStudents() {
|
||||
return new Gson().toJson(students);
|
||||
}
|
||||
|
||||
@PostMapping("/api/v1/student")
|
||||
public ResponseEntity<String> createStudent(@RequestBody String body) {
|
||||
|
||||
try {
|
||||
|
||||
Student student = new Gson().fromJson(body, Student.class);
|
||||
|
||||
students.put(student.getId(), student);
|
||||
|
||||
} catch (JsonSyntaxException e) {
|
||||
return new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
return ResponseEntity.ok("Student created.");
|
||||
}
|
||||
|
||||
@GetMapping("/api/v1/student/{id}")
|
||||
public ResponseEntity<String> getStudent(@PathVariable(name = "id") String id) {
|
||||
Student student = students.get(Integer.parseInt(id));
|
||||
|
||||
if (null == student) {
|
||||
return new ResponseEntity<String>("Student not found", HttpStatus.NOT_FOUND);
|
||||
} else {
|
||||
return ResponseEntity.ok(new Gson().toJson(student));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package me.srvstr.rest;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package me.srvstr.rest.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Student {
|
||||
|
||||
private int id;
|
||||
private int matriculationNumber;
|
||||
private String name;
|
||||
|
||||
// Set to current time.
|
||||
private final Date creationDate = new Date();
|
||||
|
||||
public Student(int id, String name, int matriculationNumber) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.matriculationNumber = matriculationNumber;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getMatriculationNumber() {
|
||||
return matriculationNumber;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Date getCreationDate() {
|
||||
return creationDate;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
meta {
|
||||
name: Create Student
|
||||
type: http
|
||||
seq: 5
|
||||
}
|
||||
|
||||
post {
|
||||
url: http://localhost:8080/api/v1/student
|
||||
body: json
|
||||
auth: none
|
||||
}
|
||||
|
||||
body:json {
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Peter Maier",
|
||||
"matriculationNumber": 123456
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
meta {
|
||||
name: Get Student
|
||||
type: http
|
||||
seq: 6
|
||||
}
|
||||
|
||||
get {
|
||||
url: http://localhost:8080/api/v1/student/1
|
||||
body: none
|
||||
auth: none
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
meta {
|
||||
name: Hello World
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
get {
|
||||
url: api/v0/hello-world
|
||||
body: none
|
||||
auth: none
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
meta {
|
||||
name: Student
|
||||
type: http
|
||||
seq: 3
|
||||
}
|
||||
|
||||
get {
|
||||
url: http://localhost:8080/api/v0/student
|
||||
body: none
|
||||
auth: none
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
meta {
|
||||
name: Students
|
||||
type: http
|
||||
seq: 4
|
||||
}
|
||||
|
||||
get {
|
||||
url: http://localhost:8080/api/v1/students
|
||||
body: none
|
||||
auth: none
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"version": "1",
|
||||
"name": "verteilte-systeme-rest",
|
||||
"type": "collection",
|
||||
"ignore": [
|
||||
"node_modules",
|
||||
".git"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
vars {
|
||||
}
|
Loading…
Reference in New Issue