added running kontor-javalin with example API
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package de.thpeetz.kontor.api;
|
||||
|
||||
import de.thpeetz.kontor.services.inmemory.InMemoryPersonReader;
|
||||
import io.javalin.Javalin;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class Main {
|
||||
private static Logger logger = LoggerFactory.getLogger(Main.class);
|
||||
private static short port = 8400;
|
||||
|
||||
public static void main(String[] args) {
|
||||
var personReader = new InMemoryPersonReader();
|
||||
var objMapper = new ObjectMapper();
|
||||
var result = objMapper.valueToTree(personReader.getAll());
|
||||
|
||||
logger.info("API: found {} people.", personReader.getAll().size());
|
||||
|
||||
var app = Javalin.create().start(port);
|
||||
app.get("/ping", ctx -> ctx.result("pong"));
|
||||
app.get("/persons", ctx -> {
|
||||
logger.info("persons called");
|
||||
ctx.json(result);
|
||||
});
|
||||
|
||||
logger.info("API's alive for real :-)))");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package de.thpeetz.kontor.models;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package de.thpeetz.kontor.services.api;
|
||||
|
||||
import de.thpeetz.kontor.models.Person;
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonReader {
|
||||
List<Person> getAll();
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package de.thpeetz.kontor.services.inmemory;
|
||||
|
||||
import de.thpeetz.kontor.models.Person;
|
||||
import java.util.List;
|
||||
|
||||
import de.thpeetz.kontor.services.api.PersonReader;
|
||||
|
||||
public class InMemoryPersonReader implements PersonReader {
|
||||
|
||||
@Override
|
||||
public List<Person> getAll() {
|
||||
return List.of(
|
||||
new Person("Vincent Vega", 73),
|
||||
new Person("Jules Winnfield", 12));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user