Skip to content

Commit 6236b8d

Browse files
authored
Update README.md
1 parent c749d8e commit 6236b8d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,47 @@ https://www.stubbornjava.com/
55

66
This is very much a work in progress and in the early stages. No code will be pushed to maven or supported in any way currently. Feel free to clone and install locally. Currently the website itself is not open souced but that is the eventual plan. The website is built using all the methods described on the site and in the examples. There is no backing blog framework.
77

8+
## Quick Example (full example [Simple REST server in Undertow](https://www.stubbornjava.com/posts/lightweight-embedded-java-rest-server-without-a-framework))
9+
10+
```java
11+
public static void createUser(HttpServerExchange exchange) {
12+
User userInput = userRequests.user(exchange);
13+
User user = userDao.create(userInput.getEmail(), userInput.getRoles());
14+
if (null == user) {
15+
ApiHandlers.badRequest(exchange, String.format("User %s already exists.", userInput.getEmail()));
16+
return;
17+
}
18+
exchange.setStatusCode(StatusCodes.CREATED);
19+
Exchange.body().sendJson(exchange, user);
20+
}
21+
22+
public static void getUser(HttpServerExchange exchange) {
23+
String email = userRequests.email(exchange);
24+
User user = userDao.get(email);
25+
if (null == user) {
26+
ApiHandlers.notFound(exchange, String.format("User %s not found.", email));
27+
return;
28+
}
29+
Exchange.body().sendJson(exchange, user);
30+
}
31+
32+
public static final RoutingHandler ROUTES = new RoutingHandler()
33+
.get("/users/{email}", timed("getUser", UserRoutes::getUser))
34+
.post("/users", timed("createUser", UserRoutes::createUser))
35+
.get("/metrics", timed("metrics", CustomHandlers::metrics))
36+
.get("/health", timed("health", CustomHandlers::health))
37+
.setFallbackHandler(timed("notFound", RoutingHandlers::notFoundHandler));
38+
39+
public static final HttpHandler ROOT = CustomHandlers.exception(EXCEPTION_THROWER)
40+
.addExceptionHandler(ApiException.class, ApiHandlers::handleApiException)
41+
.addExceptionHandler(Throwable.class, ApiHandlers::serverError);
42+
43+
public static void main(String[] args) {
44+
SimpleServer server = SimpleServer.simpleServer(Middleware.common(ROOT));
45+
server.start();
46+
}
47+
```
48+
849
# Suggest a Topic
950
Check out [issues](https://github.com/StubbornJava/StubbornJava/issues) to suggest topics, bug fixes, errors, or vote on issues. Reactions / comments may influence the order topics are added but no guarantees. Several topics will not be accepted here such as larger frameworks (Spring, Play, Jersey ...) as well as dependency injection. We will be more focused on rolling things yourself and solving practical problems. The coding style here may not fit with the norm but it should be very easy to convert any of the classes to be DI friendly.
1051

0 commit comments

Comments
 (0)